DaveM2430 commited on
Commit
d8fb997
Β·
verified Β·
1 Parent(s): 3390204

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +8 -4
app.R CHANGED
@@ -115,12 +115,15 @@ clean_pitch_type <- function(pt) {
115
 
116
  # ── Build pitcher table from leaderboard ───────────────────────────────────────
117
  build_pitcher_table <- function(lb, min_pitches, portal_only) {
 
 
118
  df <- lb %>%
119
  filter(Pitches >= min_pitches) %>%
120
  mutate(
121
  `Plot` = paste0('<button class="plot-btn" data-pitcher="',
122
  gsub('"', '&quot;', Pitcher),
123
  '">&#128202; View</button>'),
 
124
  `Twitter` = paste0('<a href="https://x.com/search?q=',
125
  gsub(" ", "+", Name),
126
  '&src=typed_query&f=user" target="_blank">&#x1F426; Search</a>'),
@@ -158,11 +161,12 @@ build_pitcher_table <- function(lb, min_pitches, portal_only) {
158
  }
159
 
160
  build_hitter_table <- function(lb, min_pa, portal_only, bat_side, hitter_pos) {
161
- latest_date <- max(lb$DateAdded, na.rm = TRUE)
 
162
  df <- lb %>%
163
  filter(PA >= min_pa) %>%
164
  mutate(
165
- is_new = !is.na(DateAdded) & DateAdded == latest_date,
166
  Name = paste0('<button class="hitter-name-btn" data-batter="',
167
  gsub('"', '&quot;', Batter),
168
  '" style="background:none;border:none;color:#002147;',
@@ -632,7 +636,7 @@ server <- function(input, output, session) {
632
  df <- df %>% filter(HomeState %in% p_states)
633
  if (!is.null(input$throws) && input$throws != "All")
634
  df <- df %>% filter(Throws == input$throws)
635
- if (isTRUE(input$new_only)) {
636
  latest_date <- max(df$DateAdded, na.rm = TRUE)
637
  df <- df %>% filter(!is.na(DateAdded) & DateAdded == latest_date)
638
  }
@@ -653,7 +657,7 @@ server <- function(input, output, session) {
653
  h_states <- input$hitter_state
654
  if (!is.null(h_states) && !("All" %in% h_states) && length(h_states) > 0)
655
  df <- df %>% filter(HomeState %in% h_states)
656
- if (isTRUE(input$new_only)) {
657
  latest_date <- max(df$DateAdded, na.rm = TRUE)
658
  df <- df %>% filter(!is.na(DateAdded) & DateAdded == latest_date)
659
  }
 
115
 
116
  # ── Build pitcher table from leaderboard ───────────────────────────────────────
117
  build_pitcher_table <- function(lb, min_pitches, portal_only) {
118
+ has_date <- "DateAdded" %in% names(lb) && any(!is.na(lb$DateAdded))
119
+ latest_date <- if (has_date) max(lb$DateAdded, na.rm = TRUE) else NA_character_
120
  df <- lb %>%
121
  filter(Pitches >= min_pitches) %>%
122
  mutate(
123
  `Plot` = paste0('<button class="plot-btn" data-pitcher="',
124
  gsub('"', '&quot;', Pitcher),
125
  '">&#128202; View</button>'),
126
+ is_new = if (has_date) (!is.na(DateAdded) & DateAdded == latest_date) else FALSE,
127
  `Twitter` = paste0('<a href="https://x.com/search?q=',
128
  gsub(" ", "+", Name),
129
  '&src=typed_query&f=user" target="_blank">&#x1F426; Search</a>'),
 
161
  }
162
 
163
  build_hitter_table <- function(lb, min_pa, portal_only, bat_side, hitter_pos) {
164
+ has_date <- "DateAdded" %in% names(lb) && any(!is.na(lb$DateAdded))
165
+ latest_date <- if (has_date) max(lb$DateAdded, na.rm = TRUE) else NA_character_
166
  df <- lb %>%
167
  filter(PA >= min_pa) %>%
168
  mutate(
169
+ is_new = if (has_date) (!is.na(DateAdded) & DateAdded == latest_date) else FALSE,
170
  Name = paste0('<button class="hitter-name-btn" data-batter="',
171
  gsub('"', '&quot;', Batter),
172
  '" style="background:none;border:none;color:#002147;',
 
636
  df <- df %>% filter(HomeState %in% p_states)
637
  if (!is.null(input$throws) && input$throws != "All")
638
  df <- df %>% filter(Throws == input$throws)
639
+ if (isTRUE(input$new_only) && "DateAdded" %in% names(df) && any(!is.na(df$DateAdded))) {
640
  latest_date <- max(df$DateAdded, na.rm = TRUE)
641
  df <- df %>% filter(!is.na(DateAdded) & DateAdded == latest_date)
642
  }
 
657
  h_states <- input$hitter_state
658
  if (!is.null(h_states) && !("All" %in% h_states) && length(h_states) > 0)
659
  df <- df %>% filter(HomeState %in% h_states)
660
+ if (isTRUE(input$new_only) && "DateAdded" %in% names(df) && any(!is.na(df$DateAdded))) {
661
  latest_date <- max(df$DateAdded, na.rm = TRUE)
662
  df <- df %>% filter(!is.na(DateAdded) & DateAdded == latest_date)
663
  }