Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
|
@@ -40,6 +40,49 @@ pitch_colors <- c(
|
|
| 40 |
"Other" = "#D3D3D3"
|
| 41 |
)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# Function to parse bat tracking JSON
|
| 44 |
parse_bat_tracking_json <- function(json_path) {
|
| 45 |
tryCatch({
|
|
@@ -613,6 +656,9 @@ server <- function(input, output, session) {
|
|
| 613 |
quote = input$quote,
|
| 614 |
stringsAsFactors = FALSE)
|
| 615 |
|
|
|
|
|
|
|
|
|
|
| 616 |
csv_data_raw(df)
|
| 617 |
|
| 618 |
# If we already have bat tracking data, try to merge
|
|
@@ -704,6 +750,7 @@ server <- function(input, output, session) {
|
|
| 704 |
paste(" Game ID:", game_id),
|
| 705 |
paste(" Rows:", nrow(df)),
|
| 706 |
paste(" Columns:", ncol(df)),
|
|
|
|
| 707 |
sep = "\n"
|
| 708 |
)
|
| 709 |
})
|
|
@@ -918,6 +965,7 @@ server <- function(input, output, session) {
|
|
| 918 |
removed_cols_text,
|
| 919 |
bat_tracking_text,
|
| 920 |
"✓ Duplicates removed",
|
|
|
|
| 921 |
sep = "\n"
|
| 922 |
)
|
| 923 |
|
|
@@ -1330,6 +1378,7 @@ server <- function(input, output, session) {
|
|
| 1330 |
"TaggedPitchType column not available"
|
| 1331 |
}),
|
| 1332 |
bat_tracking_summary,
|
|
|
|
| 1333 |
sep = "\n"
|
| 1334 |
)
|
| 1335 |
|
|
|
|
| 40 |
"Other" = "#D3D3D3"
|
| 41 |
)
|
| 42 |
|
| 43 |
+
# Function to convert date formats (MM/DD/YYYY or M/D/YYYY to YYYY-MM-DD)
|
| 44 |
+
convert_date_format <- function(date_string) {
|
| 45 |
+
if (is.na(date_string) || date_string == "") {
|
| 46 |
+
return(NA)
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# Convert to character if not already
|
| 50 |
+
date_string <- as.character(date_string)
|
| 51 |
+
|
| 52 |
+
# Check if already in YYYY-MM-DD format
|
| 53 |
+
if (grepl("^\\d{4}-\\d{2}-\\d{2}$", date_string)) {
|
| 54 |
+
return(date_string)
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# Try to parse MM/DD/YYYY or M/D/YYYY format
|
| 58 |
+
if (grepl("^\\d{1,2}/\\d{1,2}/\\d{4}$", date_string)) {
|
| 59 |
+
parsed_date <- tryCatch({
|
| 60 |
+
as.Date(date_string, format = "%m/%d/%Y")
|
| 61 |
+
}, error = function(e) NA)
|
| 62 |
+
|
| 63 |
+
if (!is.na(parsed_date)) {
|
| 64 |
+
return(format(parsed_date, "%Y-%m-%d"))
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
# Return original if no conversion possible
|
| 69 |
+
return(date_string)
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
# Function to convert date columns in a dataframe
|
| 73 |
+
convert_date_columns <- function(df) {
|
| 74 |
+
# Common date column names in TrackMan data
|
| 75 |
+
date_columns <- c("Date", "GameDate", "UTCDate", "LocalDateTime")
|
| 76 |
+
|
| 77 |
+
for (col in date_columns) {
|
| 78 |
+
if (col %in% names(df)) {
|
| 79 |
+
df[[col]] <- sapply(df[[col]], convert_date_format, USE.NAMES = FALSE)
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
return(df)
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
# Function to parse bat tracking JSON
|
| 87 |
parse_bat_tracking_json <- function(json_path) {
|
| 88 |
tryCatch({
|
|
|
|
| 656 |
quote = input$quote,
|
| 657 |
stringsAsFactors = FALSE)
|
| 658 |
|
| 659 |
+
# Auto-convert date formats (MM/DD/YYYY to YYYY-MM-DD)
|
| 660 |
+
df <- convert_date_columns(df)
|
| 661 |
+
|
| 662 |
csv_data_raw(df)
|
| 663 |
|
| 664 |
# If we already have bat tracking data, try to merge
|
|
|
|
| 750 |
paste(" Game ID:", game_id),
|
| 751 |
paste(" Rows:", nrow(df)),
|
| 752 |
paste(" Columns:", ncol(df)),
|
| 753 |
+
"✓ Date formats auto-converted (MM/DD/YYYY → YYYY-MM-DD)",
|
| 754 |
sep = "\n"
|
| 755 |
)
|
| 756 |
})
|
|
|
|
| 965 |
removed_cols_text,
|
| 966 |
bat_tracking_text,
|
| 967 |
"✓ Duplicates removed",
|
| 968 |
+
"✓ Dates converted to YYYY-MM-DD",
|
| 969 |
sep = "\n"
|
| 970 |
)
|
| 971 |
|
|
|
|
| 1378 |
"TaggedPitchType column not available"
|
| 1379 |
}),
|
| 1380 |
bat_tracking_summary,
|
| 1381 |
+
"Date format: YYYY-MM-DD (auto-converted)",
|
| 1382 |
sep = "\n"
|
| 1383 |
)
|
| 1384 |
|