diff --git a/DESCRIPTION b/DESCRIPTION index 1f124a0..8e1af46 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,5 +32,6 @@ Suggests: rmarkdown, testthat (>= 3.0.0), pkgdown, - ggrepel + ggrepel, + utils Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index 9dae829..ccfebaf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,3 +15,4 @@ export(report_stats) export(theme_turnPlot) export(tokenize) importFrom(ggplot2,"%+replace%") +importFrom(utils,download.file) diff --git a/R/auxs.R b/R/auxs.R index 842475a..5f87e6c 100644 --- a/R/auxs.R +++ b/R/auxs.R @@ -4,19 +4,29 @@ #' Available in the public repository: #' https://github.com/elpaco-escience/ifadv #' -#' @param destfile (default = "data/ifadv.rda") Destination file +#' @param destfile (default = "ifadv.rda") Destination file #' @param clean_after (default = TRUE) Cleans the downloaded file after using it #' -#' @return Nothing, but it loads the data into the global environment +#' @return A tibble containing the IFADV dataset +#' @importFrom utils download.file #' @export #' -get_ifadv <- function(destfile = "data/ifadv.rda", clean_after = TRUE) { +get_ifadv <- function(destfile = "ifadv.rda", clean_after = TRUE) { - download.file("https://raw.github.com/elpaco-escience/ifadv/main/data/ifadv.rda", destfile, quiet = TRUE) + # We are forced to download the data into a temporary file + # and load the file into memory locally + # The snippet below makes this easy to do + if(file.exists(destfile)) { + stop("The file already exists. Choose a different destfile") + } else { + file.create(destfile) # Create a dummy file + } + # Let's not forget cleaning our mess after ourselves + on.exit(if(clean_after & file.exists(destfile)) file.remove(destfile)) - load(destfile, envir = .GlobalEnv) # Load it to the outside environment + download.file("https://raw.github.com/elpaco-escience/ifadv/main/data/ifadv.rda", destfile, quiet = TRUE) - on.exit( # Clean your mess after yourself - if(clean_after & file.exists(destfile)) file.remove(destfile) - ) + e <- new.env() + load(destfile, envir = e) # Load it to the function environment ... + return(e[["ifadv"]]) # ... and return it as an output } diff --git a/man/get_ifadv.Rd b/man/get_ifadv.Rd index c771225..e5088a2 100644 --- a/man/get_ifadv.Rd +++ b/man/get_ifadv.Rd @@ -4,7 +4,7 @@ \alias{get_ifadv} \title{Get IFADV data} \usage{ -get_ifadv(destfile = "data/ifadv.rda", clean_after = TRUE) +get_ifadv(destfile = "ifadv.rda", clean_after = TRUE) } \arguments{ \item{destfile}{(default = "data/ifadv.rda") Destination file} diff --git a/tests/testthat/test-plots.R b/tests/testthat/test-plots.R index ac3661a..f918ac6 100644 --- a/tests/testthat/test-plots.R +++ b/tests/testthat/test-plots.R @@ -12,8 +12,7 @@ # between new and reference image. They are clickable from the test menu. # Load the test data -get_ifadv(destfile = "../../data/ifadv.rda") -testdata <- ifadv +testdata <- get_ifadv() #' Auxiliary decorator #' diff --git a/tests/testthat/test-report.R b/tests/testthat/test-report.R index b113380..ec4747f 100644 --- a/tests/testthat/test-report.R +++ b/tests/testthat/test-report.R @@ -1,8 +1,8 @@ -## set up the test environment -get_ifadv(destfile = "../../data/ifadv.rda") -testdata <- ifadv +## Load the test data +testdata <- get_ifadv() test_that("summary reports are accurate", { + testdata <- get_ifadv(destfile = "../../data/ifadv.rda") expect_snapshot( report_stats( testdata diff --git a/vignettes/workflows.Rmd b/vignettes/workflows.Rmd index 6b4cfad..5d5ccf6 100644 --- a/vignettes/workflows.Rmd +++ b/vignettes/workflows.Rmd @@ -33,8 +33,8 @@ The snippet below initializes the talkr dataset using the ifadv data. For more i ```{r} -data <- init(get_ifadv(destfile = "../data/ifadv.rda")) - +data <- get_ifadv() +data <- init(data) ``` Essential to any `talkr` workflow is a minimal set of data fields. These are the following: