Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
|
@@ -694,6 +694,7 @@ server <- function(input, output, session) {
|
|
| 694 |
csv_data_raw <- reactiveVal(NULL)
|
| 695 |
bat_tracking_parsed <- reactiveVal(NULL)
|
| 696 |
merge_result <- reactiveVal(NULL)
|
|
|
|
| 697 |
|
| 698 |
# Handle column selection buttons
|
| 699 |
observeEvent(input$select_all_cols, {
|
|
@@ -1527,6 +1528,84 @@ removeModal()
|
|
| 1527 |
write.csv(processed_data(), file, row.names = FALSE)
|
| 1528 |
}
|
| 1529 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1530 |
}
|
| 1531 |
|
| 1532 |
# Run the app
|
|
|
|
| 694 |
csv_data_raw <- reactiveVal(NULL)
|
| 695 |
bat_tracking_parsed <- reactiveVal(NULL)
|
| 696 |
merge_result <- reactiveVal(NULL)
|
| 697 |
+
scraped_data <- reactiveVal(NULL)
|
| 698 |
|
| 699 |
# Handle column selection buttons
|
| 700 |
observeEvent(input$select_all_cols, {
|
|
|
|
| 1528 |
write.csv(processed_data(), file, row.names = FALSE)
|
| 1529 |
}
|
| 1530 |
)
|
| 1531 |
+
|
| 1532 |
+
|
| 1533 |
+
|
| 1534 |
+
#SCRAPER STUFF
|
| 1535 |
+
|
| 1536 |
+
#Handles the middle column where it is dynamically based off the left column
|
| 1537 |
+
output$scrape_options <- renderUI({
|
| 1538 |
+
switch(input$scrape_source,
|
| 1539 |
+
"pbp" = tagList(
|
| 1540 |
+
p("Scrapes TrackMan play-by-play data from FTP.")
|
| 1541 |
+
),
|
| 1542 |
+
"pos" = tagList(
|
| 1543 |
+
p("Scrapes TrackMan player positioning data from FTP.")
|
| 1544 |
+
),
|
| 1545 |
+
"ncaa" = tagList(
|
| 1546 |
+
selectInput("ncaa_division", "Division:", choices = c("D1", "D2", "D3")),
|
| 1547 |
+
p("Scrapes NCAA scoreboard data via API.")
|
| 1548 |
+
)
|
| 1549 |
+
)
|
| 1550 |
+
})
|
| 1551 |
+
|
| 1552 |
+
# Scrape button
|
| 1553 |
+
observeEvent(input$scrape_btn, {
|
| 1554 |
+
if (input$start_date > input$end_date) {
|
| 1555 |
+
scrape_status_msg("Error: Start date must be before end date.")
|
| 1556 |
+
return()
|
| 1557 |
+
}
|
| 1558 |
+
|
| 1559 |
+
n_days <- as.numeric(input$end_date - input$start_date) + 1
|
| 1560 |
+
scrape_status_msg(paste0("Scraping ", n_days, " day(s)... please wait."))
|
| 1561 |
+
|
| 1562 |
+
result <- tryCatch({
|
| 1563 |
+
if (input$scrape_source == "pbp") {
|
| 1564 |
+
scrape_pbp(input$start_date, input$end_date)
|
| 1565 |
+
} else if (input$scrape_source == "pos") {
|
| 1566 |
+
scrape_positional(input$start_date, input$end_date)
|
| 1567 |
+
} else if (input$scrape_source == "ncaa") {
|
| 1568 |
+
# placeholder — put your NCAA scraper here
|
| 1569 |
+
scrape_status_msg("NCAA scraper not yet implemented.")
|
| 1570 |
+
NULL
|
| 1571 |
+
}
|
| 1572 |
+
}, error = function(e) {
|
| 1573 |
+
scrape_status_msg(paste("Error:", e$message))
|
| 1574 |
+
NULL
|
| 1575 |
+
})
|
| 1576 |
+
|
| 1577 |
+
if (!is.null(result) && nrow(result) > 0) {
|
| 1578 |
+
scraped_data(result)
|
| 1579 |
+
scrape_status_msg(paste0("Done! ", nrow(result), " rows × ", ncol(result), " columns."))
|
| 1580 |
+
} else if (!is.null(result)) {
|
| 1581 |
+
scraped_data(NULL)
|
| 1582 |
+
scrape_status_msg("No data found for those dates.")
|
| 1583 |
+
}
|
| 1584 |
+
})
|
| 1585 |
+
|
| 1586 |
+
# Status text
|
| 1587 |
+
output$scrape_status <- renderText({ scrape_status_msg() })
|
| 1588 |
+
|
| 1589 |
+
# Preview table
|
| 1590 |
+
output$scrape_preview <- DT::renderDataTable({
|
| 1591 |
+
req(scraped_data())
|
| 1592 |
+
DT::datatable(scraped_data(), options = list(scrollX = TRUE, pageLength = 10))
|
| 1593 |
+
})
|
| 1594 |
+
|
| 1595 |
+
# Download
|
| 1596 |
+
output$download_scrape <- downloadHandler(
|
| 1597 |
+
filename = function() {
|
| 1598 |
+
label <- switch(input$scrape_source, "pbp" = "pbp", "pos" = "positional", "ncaa" = "ncaa")
|
| 1599 |
+
paste0("trackman_", label, "_",
|
| 1600 |
+
format(input$start_date, "%Y%m%d"), "_to_",
|
| 1601 |
+
format(input$end_date, "%Y%m%d"), ".csv")
|
| 1602 |
+
},
|
| 1603 |
+
content = function(file) {
|
| 1604 |
+
req(scraped_data())
|
| 1605 |
+
write.csv(scraped_data(), file, row.names = FALSE)
|
| 1606 |
+
}
|
| 1607 |
+
)
|
| 1608 |
+
|
| 1609 |
}
|
| 1610 |
|
| 1611 |
# Run the app
|