Skip to contents

nhanesR stores downloaded and parsed NHANES files in a local cache to avoid redundant downloads. By default the cache is placed in the standard user data directory for your operating system (see below). Use this function to view or change the location for the current session, or set it permanently in your .Rprofile.

Usage

nhanes_cache_dir(path = NULL, create = TRUE)

Arguments

path

Optional character. New path to use as the cache directory for the current session. If NULL, returns the current setting without changing it.

create

Logical. If TRUE (default), create the directory if it does not exist.

Value

The current (or newly set) cache directory path, invisibly.

Details

Package options

Three options control nhanesR behavior. Set any of them in your .Rprofile to make the change permanent across sessions; changes made during a session (via nhanes_cache_dir() or options() directly) last only until the session ends.

OptionDefaultPurpose
nhanesR.cache_dirfile.path(tempdir(), "nhanesR")Root directory for all cached files
nhanesR.verboseTRUEPrint progress messages during downloads
nhanesR.timeout120LHTTP request timeout in seconds

Default cache location

By default the cache lives inside R's session-temporary directory (tempdir()), so nhanesR never writes to your home directory without your consent. Downloaded files are re-fetched in each new R session. To keep a persistent cache, set nhanesR.cache_dir in your ~/.Rprofile.

Setting options permanently

Add lines like these to your ~/.Rprofile:

options(
  nhanesR.cache_dir = "/data/nhanes_cache",  # shared lab server path
  nhanesR.verbose   = FALSE,                  # suppress progress messages
  nhanesR.timeout   = 300L                    # 5-minute timeout for slow connections
)

Options set in .Rprofile take precedence over package defaults: nhanesR only sets an option at load time if it is not already defined.

See also

nhanes_download() and nhanes_download_analyte(), whose caching behavior is controlled by the options described above.

Examples

# View current cache location (defaults to a subdirectory of tempdir())
nhanes_cache_dir()
#> [1] "/var/folders/68/vh2f8kzn09j8954r6q9100yh0000gn/T//Rtmp7zLR2U/nhanesR"

# Change to a persistent location for this session only
# \donttest{
nhanes_cache_dir("~/my_nhanes_cache")
#> [1] "/Users/dwinsemius/my_nhanes_cache"
# }

# Suppress download messages for this session
options(nhanesR.verbose = FALSE)

# View all current nhanesR option values
Filter(function(x) startsWith(x, "nhanesR."), names(options()))
#> [1] "nhanesR.cache_dir" "nhanesR.timeout"   "nhanesR.verbose"