robjhyndman / cricketdata

International cricket data for men and women, Tests, ODIs and T20s
http://pkg.robjhyndman.com/cricketdata/
79 stars 21 forks source link

Issues with environment in `fetch_cricsheet` #31

Closed meysubb closed 1 year ago

meysubb commented 1 year ago

Can't use fetch_cricsheet within a function.

I believe the reason is because of how arguments are assessed in the function. It always looks in the parent environment: formal.args <- formals(sys.function(sysP <- sys.parent()))

If I decide to put fetch_cricsheet in the function, the formal arguments are always null and the function errors out

Example failure:

leagues <- c("bbl","ipl","the_hundred","t20_blast","super_smash")
full_df <- purrr::map_dfr(leagues,fetch_cricsheet,gender="male",type="bbb")

Another example failure

get_player_bowling <- function(league,player_name){
  cat(glue::glue("Processing {player_name} in {league}"))
  league_df <- fetch_cricsheet(competition = league, gender = "male", type = "bbb")

  player_df <- league_df %>% filter(grepl(player_name,bowler)) %>% 
    mutate(total_runs_conceded = runs_off_bat + extras)
  return(player_df)
}

get_player_bowling(league="ipl",player_name = "Ashwin")
meysubb commented 1 year ago

Ope, an update to the package fixed this for me, sorry to bother y'all!