This function stores your cache_dir path on your local machine and
loads it for future sessions. Type Sys.getenv("MAPSPAIN_CACHE_DIR") to
find your cached path, or use esp_detect_cache_dir().
Usage
esp_set_cache_dir(
cache_dir = NULL,
overwrite = FALSE,
install = FALSE,
verbose = TRUE
)
esp_detect_cache_dir()Arguments
- cache_dir
A path to a cache directory. When
NULL, the function stores cached files in a temporary directory (seebase::tempdir()).- overwrite
logical. If
TRUE, overwrites an existingMAPSPAIN_CACHE_DIRon your local machine.- install
logical. If
TRUE, installs the key on your local machine for use in future sessions. Defaults toFALSE. Ifcache_dirisFALSE, this argument is automatically set toFALSE.- verbose
logical. If
TRUEdisplays informational messages.
Value
esp_set_cache_dir() returns an (invisible) character string with the path
to your cache_dir. It is primarily called for its side effect.
esp_detect_cache_dir() returns the path to the cache_dir used in the
current session.
Details
By default, when no cache_dir is set, the package uses a folder inside
base::tempdir() (files are temporary and removed when the R session
ends). To persist a cache across R sessions, use
esp_set_cache_dir(cache_dir, install = TRUE), which writes the chosen
path to a configuration file under tools::R_user_dir("mapSpain", "config").
Note
In mapSpain >= 1.0.0, the configuration file location has
moved from rappdirs::user_config_dir("mapSpain", "R") to
tools::R_user_dir("mapSpain", "config"). A migration function automatically
transfers previous configuration files from the old to the new location.
A message appears once during this migration.
Caching strategies
Some files can be read from its online source without caching using the
option cache = FALSE. Otherwise the source file will be downloaded to
your computer. mapSpain implements the following caching options:
For occasional use, rely on the default
tempdir()-based cache (no install).Modify the cache for a single session by setting
esp_set_cache_dir(cache_dir = "a/path/here").For reproducible workflows, install a persistent cache with
esp_set_cache_dir(cache_dir = "a/path/here", install = TRUE)that persists across R sessions.For caching specific files, use the
cache_dirargument in the corresponding function.
Sometimes cached files may be corrupted. In that case, try re-downloading
the data by setting update_cache = TRUE in the corresponding function.
If you experience download problems, try downloading the file by another
method and save it to your cache_dir. Use verbose = TRUE to debug the
API query and esp_detect_cache_dir() to identify your cache path.
See also
Other cache utilities:
esp_clear_cache()
Examples
# Don't run this! It would modify your current state
# \dontrun{
my_cache <- esp_detect_cache_dir()
#> ℹ C:\Users\RUNNER~1\AppData\Local\Temp\Rtmpu6mf0f/mapSpain
# Set an example cache
ex <- file.path(tempdir(), "example", "cachenew")
esp_set_cache_dir(ex)
#> ℹ mapSpain cache dir is C:\Users\RUNNER~1\AppData\Local\Temp\Rtmpu6mf0f/example/cachenew.
#> ℹ To install your `cache_dir` path for use in future sessions run this function with `install = TRUE`.
esp_detect_cache_dir()
#> ℹ C:\Users\RUNNER~1\AppData\Local\Temp\Rtmpu6mf0f/example/cachenew
#> [1] "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\Rtmpu6mf0f/example/cachenew"
# Restore initial cache
esp_set_cache_dir(my_cache)
#> ℹ mapSpain cache dir is C:\Users\RUNNER~1\AppData\Local\Temp\Rtmpu6mf0f/mapSpain.
#> ℹ To install your `cache_dir` path for use in future sessions run this function with `install = TRUE`.
identical(my_cache, esp_detect_cache_dir())
#> ℹ C:\Users\RUNNER~1\AppData\Local\Temp\Rtmpu6mf0f/mapSpain
#> [1] TRUE
# }
esp_detect_cache_dir()
#> ℹ C:\Users\RUNNER~1\AppData\Local\Temp\Rtmpu6mf0f/mapSpain
#> [1] "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\Rtmpu6mf0f/mapSpain"
