diff --git a/DESCRIPTION b/DESCRIPTION index b2f2359..fca482d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,3 +25,6 @@ Suggests: knitr, rmarkdown RoxygenNote: 6.1.0 +X-schema.org-applicationCategory: Data +X-schema.org-keywords: caching, data, files, xml, pdf +X-schema.org-isPartOf: https://ropensci.org diff --git a/R/hoard_client.R b/R/hoard_client.R index 8e1d9d5..2c8139e 100644 --- a/R/hoard_client.R +++ b/R/hoard_client.R @@ -21,7 +21,7 @@ #' Get the cache path #' **return**: (character) path to the cache directory #' } -#' \item{`cache_path_set(path, type = "user_cache_dir", prefix = "R")`}{ +#' \item{`cache_path_set(path = NULL, type = "user_cache_dir", prefix = "R", full_path = NULL)`}{ #' Set the cache path. By default, we set cache path to #' `file.path(user_cache_dir, prefix, path)`. Note that this does not #' actually make the directory, but just sets the path to it. @@ -30,6 +30,8 @@ #' by `type` #' \item type (character) the type of cache, see [rappdirs] #' \item prefix (character) prefix to the `path` value. Default: "R" +#' \item full_path (character) instead of using `path`, `type`, and `prefix` +#' just set the full path with this parameter #' } #' **return**: (character) path to the cache directory just set #' } @@ -101,6 +103,13 @@ #' x #' x$path #' x$cache_path_get() +#' +#' # Or you can set the full path directly with `full_path` +#' mydir <- file.path(tempdir(), "foobar") +#' x$cache_path_set(full_path = mydir) +#' x +#' x$path +#' x$cache_path_get() #' #' # make the directory if doesn't exist already #' x$mkdir() @@ -170,10 +179,17 @@ HoardClient <- R6::R6Class( if (inherits(res, "error")) return(NULL) else res }, - cache_path_set = function(path, type = "user_cache_dir", prefix = "R") { - self$path <- path - private$hoard_env$cache_path <- - file.path(eval(parse(text = type))(), prefix, path) + cache_path_set = function(path = NULL, type = "user_cache_dir", prefix = "R", + full_path = NULL) { + + if (is.null(full_path)) { + self$path <- path + private$hoard_env$cache_path <- + file.path(eval(parse(text = type))(), prefix, path) + } else { + private$hoard_env$cache_path <- full_path + } + # return path to user self$cache_path_get() }, diff --git a/man/hoard.Rd b/man/hoard.Rd index a0c3332..28edbd9 100644 --- a/man/hoard.Rd +++ b/man/hoard.Rd @@ -28,7 +28,7 @@ itself exported, but you can use it if you want via \code{:::} Get the cache path \strong{return}: (character) path to the cache directory } -\item{\code{cache_path_set(path, type = "user_cache_dir", prefix = "R")}}{ +\item{\code{cache_path_set(path = NULL, type = "user_cache_dir", prefix = "R", full_path = NULL)}}{ Set the cache path. By default, we set cache path to \code{file.path(user_cache_dir, prefix, path)}. Note that this does not actually make the directory, but just sets the path to it. @@ -37,6 +37,8 @@ actually make the directory, but just sets the path to it. by \code{type} \item type (character) the type of cache, see \link{rappdirs} \item prefix (character) prefix to the \code{path} value. Default: "R" +\item full_path (character) instead of using \code{path}, \code{type}, and \code{prefix} +just set the full path with this parameter } \strong{return}: (character) path to the cache directory just set } @@ -108,6 +110,13 @@ x x$path x$cache_path_get() +# Or you can set the full path directly with `full_path` +mydir <- file.path(tempdir(), "foobar") +x$cache_path_set(full_path = mydir) +x +x$path +x$cache_path_get() + # make the directory if doesn't exist already x$mkdir() diff --git a/tests/testthat/test-HoardClient.R b/tests/testthat/test-HoardClient.R index 7735bb5..0ea9697 100644 --- a/tests/testthat/test-HoardClient.R +++ b/tests/testthat/test-HoardClient.R @@ -22,7 +22,7 @@ test_that("HoardClient works", { # test cache_path_set method expect_is(bb$cache_path_set, "function") - expect_error(bb$cache_path_set(), "argument \"path\" is missing") + expect_equal(length(bb$cache_path_set()), 0) expect_is( bb$cache_path_set(path = "test123", type = 'tempdir'), "character" @@ -32,6 +32,13 @@ test_that("HoardClient works", { "test123" ) + # use full_path + vv <- tempdir() + expect_match( + bb$cache_path_set(full_path = vv), + vv + ) + # clean up before testing if (dir.exists(bb$cache_path_get())) { unlink(bb$cache_path_get(), recursive = TRUE, force = TRUE)