igroffman commited on
Commit
2540ee6
·
verified ·
1 Parent(s): f87a827

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +22 -29
app.R CHANGED
@@ -753,34 +753,34 @@ create_report_contact_chart <- function(game_data, player_name) {
753
 
754
  calculate_leaderboards <- function(df, team_meta_df = team_meta) {
755
 
756
- # Helper to format names from "Last, First" to "First Last"
757
  format_name <- function(name) {
758
  if (is.na(name)) return(name)
759
  stringr::str_replace(name, "^\\s*(\\w+)\\s*,\\s*(\\w+)\\s*$", "\\2 \\1")
760
  }
761
 
762
  get_logo <- function(team_abbr) {
763
- if (is.null(team_meta_df) || is.null(team_abbr) || is.na(team_abbr) || team_abbr == "") return("")
764
- # Try exact match first
765
-
766
- idx <- which(team_meta_df$team_abbr == team_abbr)
767
- if (length(idx) > 0) return(team_meta_df$BTeamLogo[idx[1]])
768
- # Try case-insensitive match
769
- idx <- which(tolower(team_meta_df$team_abbr) == tolower(team_abbr))
770
- if (length(idx) > 0) return(team_meta_df$BTeamLogo[idx[1]])
771
- ""
772
- }
773
-
774
- get_team_name <- function(abbr) {
775
- if (is.null(team_meta_df) || is.null(abbr) || is.na(abbr) || abbr == "") return(abbr)
776
- # Try exact match first
777
- idx <- which(team_meta_df$team_abbr == abbr)
778
- if (length(idx) > 0) return(team_meta_df$BTeamName[idx[1]])
779
- # Try case-insensitive match
780
- idx <- which(tolower(team_meta_df$team_abbr) == tolower(abbr))
781
- if (length(idx) > 0) return(team_meta_df$BTeamName[idx[1]])
782
- abbr
783
- }
 
784
 
785
  # Game Info
786
  stadium <- if ("Stadium" %in% names(df)) unique(na.omit(df$Stadium))[1] else "Unknown"
@@ -827,13 +827,6 @@ game_date <- if ("Date" %in% names(df)) {
827
  team2 <- score_info$BatterTeam[2]
828
  runs2 <- score_info$Runs[2]
829
 
830
- # Get team names from TMB if available
831
- get_team_name <- function(abbr) {
832
- if (is.null(team_meta_df)) return(abbr)
833
- name <- team_meta_df$BTeamName[team_meta_df$team_abbr == abbr]
834
- if (length(name) == 0 || is.na(name[1])) return(abbr)
835
- name[1]
836
- }
837
 
838
  final_score <- paste0(get_team_name(team1), " ", runs1, " - ",
839
  get_team_name(team2), " ", runs2)
 
753
 
754
  calculate_leaderboards <- function(df, team_meta_df = team_meta) {
755
 
 
756
  format_name <- function(name) {
757
  if (is.na(name)) return(name)
758
  stringr::str_replace(name, "^\\s*(\\w+)\\s*,\\s*(\\w+)\\s*$", "\\2 \\1")
759
  }
760
 
761
  get_logo <- function(team_abbr) {
762
+ if (is.null(team_meta_df) || is.null(team_abbr) || is.na(team_abbr)) return("")
763
+ team_abbr <- trimws(as.character(team_abbr))
764
+ if (team_abbr == "") return("")
765
+ tmb_abbrs <- trimws(as.character(team_meta_df$team_abbr))
766
+ idx <- which(tmb_abbrs == team_abbr)
767
+ if (length(idx) > 0) return(team_meta_df$BTeamLogo[idx[1]])
768
+ idx <- which(tolower(tmb_abbrs) == tolower(team_abbr))
769
+ if (length(idx) > 0) return(team_meta_df$BTeamLogo[idx[1]])
770
+ ""
771
+ }
772
+
773
+ get_team_name <- function(abbr) {
774
+ if (is.null(team_meta_df) || is.null(abbr) || is.na(abbr)) return(as.character(abbr))
775
+ abbr <- trimws(as.character(abbr))
776
+ if (abbr == "") return(abbr)
777
+ tmb_abbrs <- trimws(as.character(team_meta_df$team_abbr))
778
+ idx <- which(tmb_abbrs == abbr)
779
+ if (length(idx) > 0) return(team_meta_df$BTeamName[idx[1]])
780
+ idx <- which(tolower(tmb_abbrs) == tolower(abbr))
781
+ if (length(idx) > 0) return(team_meta_df$BTeamName[idx[1]])
782
+ abbr
783
+ }
784
 
785
  # Game Info
786
  stadium <- if ("Stadium" %in% names(df)) unique(na.omit(df$Stadium))[1] else "Unknown"
 
827
  team2 <- score_info$BatterTeam[2]
828
  runs2 <- score_info$Runs[2]
829
 
 
 
 
 
 
 
 
830
 
831
  final_score <- paste0(get_team_name(team1), " ", runs1, " - ",
832
  get_team_name(team2), " ", runs2)