Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Chadwick helper functions #307

Open
beanumber opened this issue Aug 7, 2023 · 1 comment
Open

add Chadwick helper functions #307

beanumber opened this issue Aug 7, 2023 · 1 comment

Comments

@beanumber
Copy link
Contributor

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants