From 6a2f9bb15bdcb023cac850cc225d04b530141126 Mon Sep 17 00:00:00 2001 From: mrcaseb Date: Wed, 16 Aug 2023 14:23:39 +0200 Subject: [PATCH 1/5] create user cache directory with tools --- DESCRIPTION | 4 ++-- NAMESPACE | 6 +++++- NEWS.md | 4 ++++ R/cache.R | 6 +++--- R/nfl4th-package.R | 6 +++++- R/zzz.R | 14 ++++++++------ 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d85e1c9..b0e208a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: nfl4th Title: Functions to Calculate Optimal Fourth Down Decisions in the National Football League -Version: 1.0.3 +Version: 1.0.4 Authors@R: c(person(given = "Ben", family = "Baldwin", @@ -19,6 +19,7 @@ BugReports: https://github.com/nflverse/nfl4th/issues Depends: R (>= 3.6) Imports: + backports (>= 1.1.6), curl, dplyr, glue, @@ -30,7 +31,6 @@ Imports: nflfastR (>= 4.0.0), nflreadr, purrr, - rappdirs, rlang, stringr, tibble, diff --git a/NAMESPACE b/NAMESPACE index 5310ac8..2540c7d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,11 +7,15 @@ export(load_4th_pbp) export(make_2pt_table_data) export(make_table_data) export(nfl4th_clear_cache) +if (getRversion() >= "4.0.0") { + importFrom(tools, R_user_dir) +} else { + importFrom(backports, R_user_dir) +} import(dplyr) importFrom(glue,glue) importFrom(magrittr,"%>%") importFrom(nflfastR,calculate_win_probability) -importFrom(rappdirs,user_cache_dir) importFrom(rlang,arg_match) importFrom(stringr,str_replace_all) importFrom(tidyr,pivot_wider) diff --git a/NEWS.md b/NEWS.md index 4acaf6d..3194a3a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# nfl4th 1.0.4 + +* Create package cache directory with `tools::R_user_dir()` because CRAN doesn't like `rappdirs::user_cache_dir()` + # nfl4th 1.0.3 * Re-export xgboost models to get rid of annoying warning message diff --git a/R/cache.R b/R/cache.R index 65775f9..03b1e52 100644 --- a/R/cache.R +++ b/R/cache.R @@ -1,9 +1,9 @@ # paths are defined in zzz.R # these helpers read games or fd_model and save them to a package cache -nfl4th_games_path <- function() file.path(rappdirs::user_cache_dir("nfl4th", "nflverse"), "games_nfl4th.rds") -nfl4th_fdmodel_path <- function() file.path(rappdirs::user_cache_dir("nfl4th", "nflverse"), "fd_model.rds") -nfl4th_wpmodel_path <- function() file.path(rappdirs::user_cache_dir("nfl4th", "nflverse"), "wp_model.rds") +nfl4th_games_path <- function() file.path(R_user_dir("nfl4th", "cache"), "games_nfl4th.rds") +nfl4th_fdmodel_path <- function() file.path(R_user_dir("nfl4th", "cache"), "fd_model.rds") +nfl4th_wpmodel_path <- function() file.path(R_user_dir("nfl4th", "cache"), "wp_model.rds") .games_nfl4th <- function(){ if (!file.exists(nfl4th_games_path())){ diff --git a/R/nfl4th-package.R b/R/nfl4th-package.R index 4982ee3..0a903b7 100644 --- a/R/nfl4th-package.R +++ b/R/nfl4th-package.R @@ -8,11 +8,15 @@ #' @importFrom glue glue #' @importFrom magrittr %>% #' @importFrom nflfastR calculate_win_probability -#' @importFrom rappdirs user_cache_dir #' @importFrom rlang arg_match #' @importFrom stringr str_replace_all #' @importFrom tidyr pivot_wider #' @importFrom tidyselect any_of #' @importFrom xgboost getinfo +#' @rawNamespace if (getRversion() >= "4.0.0") { +#' importFrom(tools, R_user_dir) +#' } else { +#' importFrom(backports, R_user_dir) +#' } ## usethis namespace: end NULL diff --git a/R/zzz.R b/R/zzz.R index 6c74333..e34f1ba 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,15 +1,17 @@ .onLoad <- function(libname,pkgname){ - + + backports::import(pkgname, "R_user_dir") + is_online <- !is.null(curl::nslookup("github.com", error = FALSE)) keep_games <- isTRUE(getOption("nfl4th.keep_games", FALSE)) - - if(!is_online && !keep_games) rlang::warn("GitHub.com seems offline, and `options(nfl4th.keep_games)` is not set to TRUE. Deleting the games cache, and predictions may not be available without an internet connection.") - + + if(!is_online && !keep_games) rlang::warn("GitHub.com seems offline, and `options(nfl4th.keep_games)` is not set to TRUE. Deleting the games cache, and predictions may not be available without an internet connection.") + if(!is_online && keep_games) rlang::warn("GitHub.com seems offline, and `options(nfl4th.keep_games)` is set to TRUE. To get updates, clear the games cache with `nfl4th::nfl4th_clear_cache()`") # create package cache directory if it doesn't exist - if (!dir.exists(rappdirs::user_cache_dir("nfl4th", "nflverse"))){ - dir.create(rappdirs::user_cache_dir("nfl4th", "nflverse"), recursive = TRUE, showWarnings = FALSE) + if (!dir.exists(R_user_dir("nfl4th", "cache"))){ + dir.create(R_user_dir("nfl4th", "cache"), recursive = TRUE, showWarnings = FALSE) } else if (file.exists(nfl4th_games_path()) && !keep_games){ # remove games from package cache on load so it updates # only runs if options(nfl4th.keep_games) != TRUE From c666c3ed83ef5aa9d5aba51741ffd8f993926eac Mon Sep 17 00:00:00 2001 From: Sebastian Carl Date: Wed, 16 Aug 2023 15:44:31 +0200 Subject: [PATCH 2/5] submission --- CRAN-SUBMISSION | 3 +++ DESCRIPTION | 2 +- cran-comments.md | 5 +---- man/nfl4th-package.Rd | 1 + 4 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 CRAN-SUBMISSION diff --git a/CRAN-SUBMISSION b/CRAN-SUBMISSION new file mode 100644 index 0000000..dd0fff9 --- /dev/null +++ b/CRAN-SUBMISSION @@ -0,0 +1,3 @@ +Version: 1.0.4 +Date: 2023-08-16 13:44:06 UTC +SHA: 6a2f9bb15bdcb023cac850cc225d04b530141126 diff --git a/DESCRIPTION b/DESCRIPTION index b0e208a..c959f50 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,7 +14,7 @@ Description: A set of functions to estimate outcomes of fourth down plays in the National Football League and obtain fourth down plays from and . License: MIT + file LICENSE -URL: https://www.nfl4th.com/, https://github.com/nflverse/nfl4th/ +URL: https://www.nfl4th.com/, https://github.com/nflverse/nfl4th/, https://github.com/nflverse/nfl4th BugReports: https://github.com/nflverse/nfl4th/issues Depends: R (>= 3.6) diff --git a/cran-comments.md b/cran-comments.md index 910e4fe..27aefe4 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,9 +1,6 @@ ## Release summary -This is a minor release that - -* Uses cache to allow the package to function without internet. -* Removes tidyverse from Suggests +Create user cache directory with tools instead of rappdirs ## Test environments * Windows Server 2022, R-devel, 64 bit diff --git a/man/nfl4th-package.Rd b/man/nfl4th-package.Rd index 522fcb7..335641a 100644 --- a/man/nfl4th-package.Rd +++ b/man/nfl4th-package.Rd @@ -15,6 +15,7 @@ Useful links: \itemize{ \item \url{https://www.nfl4th.com/} \item \url{https://github.com/nflverse/nfl4th/} + \item \url{https://github.com/nflverse/nfl4th} \item Report bugs at \url{https://github.com/nflverse/nfl4th/issues} } From 22b7132039f535109b40b4eeed213327ed50fa2a Mon Sep 17 00:00:00 2001 From: mrcaseb Date: Thu, 17 Aug 2023 18:43:28 +0200 Subject: [PATCH 3/5] skip cache on cran --- R/cache.R | 12 ++++++++++++ cran-comments.md | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/R/cache.R b/R/cache.R index 03b1e52..e46076e 100644 --- a/R/cache.R +++ b/R/cache.R @@ -6,6 +6,7 @@ nfl4th_fdmodel_path <- function() file.path(R_user_dir("nfl4th", "cache"), "fd_m nfl4th_wpmodel_path <- function() file.path(R_user_dir("nfl4th", "cache"), "wp_model.rds") .games_nfl4th <- function(){ + if (probably_cran()) return(get_games_file()) if (!file.exists(nfl4th_games_path())){ saveRDS(get_games_file(), nfl4th_games_path()) } @@ -13,6 +14,7 @@ nfl4th_wpmodel_path <- function() file.path(R_user_dir("nfl4th", "cache"), "wp_m } fd_model <- function(){ + if (probably_cran()) return(load_fd_model()) if (!file.exists(nfl4th_fdmodel_path())){ saveRDS(load_fd_model(), nfl4th_fdmodel_path()) } @@ -20,6 +22,7 @@ fd_model <- function(){ } wp_model <- function(){ + if (probably_cran()) return(load_wp_model()) if (!file.exists(nfl4th_wpmodel_path())){ saveRDS(load_wp_model(), nfl4th_wpmodel_path()) } @@ -52,3 +55,12 @@ nfl4th_clear_cache <- function(type = c("games", "fd_model", "wp_model", "all")) file.remove(to_delete[file.exists(to_delete)]) invisible(TRUE) } + +# The env var _R_CHECK_EXAMPLE_TIMING_CPU_TO_ELAPSED_THRESHOLD_ is mostly +# a CRAN env var. We use it to decide if the code likely is running on CRAN +probably_cran <- function(){ + cpu_threshold <- Sys.getenv( + "_R_CHECK_EXAMPLE_TIMING_CPU_TO_ELAPSED_THRESHOLD_", NA_character_ + ) + !is.na(cpu_threshold) +} diff --git a/cran-comments.md b/cran-comments.md index 27aefe4..6f62f2d 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,8 +1,9 @@ ## Release summary -Create user cache directory with tools instead of rappdirs +Create user cache directory with tools instead of rappdirs and make sure it's cleared ## Test environments + * Windows Server 2022, R-devel, 64 bit * Fedora Linux, R-devel, clang, gfortran * Ubuntu Linux 20.04.1 LTS, R-release, GCC From 9a0222a689b872afef2864b5c45d27554a0b9182 Mon Sep 17 00:00:00 2001 From: mrcaseb Date: Thu, 17 Aug 2023 18:54:28 +0200 Subject: [PATCH 4/5] allow user to force the cache even if probably_cran() is TRUE --- R/cache.R | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/R/cache.R b/R/cache.R index e46076e..d57c998 100644 --- a/R/cache.R +++ b/R/cache.R @@ -6,7 +6,7 @@ nfl4th_fdmodel_path <- function() file.path(R_user_dir("nfl4th", "cache"), "fd_m nfl4th_wpmodel_path <- function() file.path(R_user_dir("nfl4th", "cache"), "wp_model.rds") .games_nfl4th <- function(){ - if (probably_cran()) return(get_games_file()) + if (probably_cran() && !force_cache()) return(get_games_file()) if (!file.exists(nfl4th_games_path())){ saveRDS(get_games_file(), nfl4th_games_path()) } @@ -14,7 +14,7 @@ nfl4th_wpmodel_path <- function() file.path(R_user_dir("nfl4th", "cache"), "wp_m } fd_model <- function(){ - if (probably_cran()) return(load_fd_model()) + if (probably_cran() && !force_cache()) return(load_fd_model()) if (!file.exists(nfl4th_fdmodel_path())){ saveRDS(load_fd_model(), nfl4th_fdmodel_path()) } @@ -22,7 +22,7 @@ fd_model <- function(){ } wp_model <- function(){ - if (probably_cran()) return(load_wp_model()) + if (probably_cran() && !force_cache()) return(load_wp_model()) if (!file.exists(nfl4th_wpmodel_path())){ saveRDS(load_wp_model(), nfl4th_wpmodel_path()) } @@ -64,3 +64,8 @@ probably_cran <- function(){ ) !is.na(cpu_threshold) } + +# allow user to force the cache even if probably_cran() is TRUE +force_cache <- function() { + getOption("nfl4th.force_cache", "false") == "true" +} From 25fccea1924e96faa03687d50db0ddfec1338544 Mon Sep 17 00:00:00 2001 From: Sebastian Carl Date: Fri, 18 Aug 2023 16:05:44 +0200 Subject: [PATCH 5/5] submit again --- CRAN-SUBMISSION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CRAN-SUBMISSION b/CRAN-SUBMISSION index dd0fff9..4928a68 100644 --- a/CRAN-SUBMISSION +++ b/CRAN-SUBMISSION @@ -1,3 +1,3 @@ Version: 1.0.4 -Date: 2023-08-16 13:44:06 UTC -SHA: 6a2f9bb15bdcb023cac850cc225d04b530141126 +Date: 2023-08-18 14:05:13 UTC +SHA: 9a0222a689b872afef2864b5c45d27554a0b9182