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

create user cache directory with tools #29

Merged
merged 5 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Version: 1.0.4
Date: 2023-08-18 14:05:13 UTC
SHA: 9a0222a689b872afef2864b5c45d27554a0b9182
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -14,11 +14,12 @@ Description: A set of functions to estimate outcomes of fourth down
plays in the National Football League and obtain fourth down plays
from <https://www.nfl.com/> and <https://www.espn.com/>.
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)
Imports:
backports (>= 1.1.6),
curl,
dplyr,
glue,
Expand All @@ -30,7 +31,6 @@ Imports:
nflfastR (>= 4.0.0),
nflreadr,
purrr,
rappdirs,
rlang,
stringr,
tibble,
Expand Down
6 changes: 5 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 20 additions & 3 deletions R/cache.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
# 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 (probably_cran() && !force_cache()) return(get_games_file())
if (!file.exists(nfl4th_games_path())){
saveRDS(get_games_file(), nfl4th_games_path())
}
readRDS(nfl4th_games_path())
}

fd_model <- function(){
if (probably_cran() && !force_cache()) return(load_fd_model())
if (!file.exists(nfl4th_fdmodel_path())){
saveRDS(load_fd_model(), nfl4th_fdmodel_path())
}
readRDS(nfl4th_fdmodel_path())
}

wp_model <- function(){
if (probably_cran() && !force_cache()) return(load_wp_model())
if (!file.exists(nfl4th_wpmodel_path())){
saveRDS(load_wp_model(), nfl4th_wpmodel_path())
}
Expand Down Expand Up @@ -52,3 +55,17 @@ 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)
}

# allow user to force the cache even if probably_cran() is TRUE
force_cache <- function() {
getOption("nfl4th.force_cache", "false") == "true"
}
6 changes: 5 additions & 1 deletion R/nfl4th-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 8 additions & 6 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 2 additions & 4 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
## 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 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
Expand Down
1 change: 1 addition & 0 deletions man/nfl4th-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.