BillPetti / baseballr

A package written for R focused on baseball analysis. Currently in development.
billpetti.github.io/baseballr
Other
367 stars 100 forks source link

add Chadwick helper functions #307

Open beanumber opened 1 year ago

beanumber commented 1 year ago

Getting Chadwick to work on Linux (and probably Mac) requires setting the LD_LIBRARY_PATH environment variable. This process is not well-documented to begin with, and then the use of system() to call cwevent depends on the environment variable being set correctly, which it's not by default. Without this variable set, retrosheet_data() doesn't work.

I wrote these functions to help me debug my own installation, and I think they would be helpful for others. Adding a call to chadwick_ld_library_path() inside retrosheet_data() would hopefully make this work seamlessly for more people.

I'd be happy to work this up into a formal pull request if you'll consider it.

chadwick_installed <- function() {
  out <- fs::path_dir(system2("which", "cwevent", stdout = TRUE))
  if (!fs::dir_exists(out)) {
    stop("Chadwick could not be find.")
  } else {
    return(fs::path(out))
  }
}
chadwick_installed()

chadwick_find_lib <- function() {
  system2(
    "find", 
    '/usr/local -name "libchadwick*"', stdout = TRUE
  ) |>
    dirname() |>
    unique()
}
chadwick_find_lib()

chadwick_set_ld_library_path <- function() {
  new_ld_library_path <- paste(
    chadwick_find_lib(), 
    Sys.getenv("LD_LIBRARY_PATH"), 
    sep = ":"
  )
  Sys.setenv(LD_LIBRARY_PATH = new_ld_library_path)
}

chadwick_ld_library_path <- function() {
  ld_library_paths <- Sys.getenv("LD_LIBRARY_PATH") |>
    stringr::str_split_1(pattern = ":")
  if (!chadwick_find_lib() %in% ld_library_paths) {
    chadwick_set_ld_library_path()
  }
  chadwick_find_lib() %in% ld_library_paths
}
chadwick_ld_library_path()

# 2016 Retrosheet play-by-play data

chadwick_ld_library_path()
retro_data <- baseballr::retrosheet_data(
  here::here("data_large/retrosheet"),
  c(1998, 2016)
)
saiemgilani commented 1 year ago

Yes, please. It was incredibly confusing trying to get this to work