cache.setup: Sets up a cache directory for storing model inputs and...

View source: R/cache.setup.R

cache.setupR Documentation

Sets up a cache directory for storing model inputs and outputs

Description

Using save_out = TRUE or check = TRUE from cfa.from.keys(), bifactor.from.keys(), efa.from.keys(), or esem.from.mods() requires a cache directory to save model objects to or to check against. By default, cache.setup uses the system's default cache location, otherwise a location within the current working directory or project can be selected. The function needs to be run once after the environment is cleared whenever a cache directory is required.

Usage

cache.setup(location = "user", interactive = TRUE)

Arguments

location

The cache location to save model objects to to enable checking. If location = "user" (default, recommended if unsure), then the system's default cache location is used. If any other string is used a folder will be created within either the getwd() directory or, if available, within the directory identified by here::here().

interactive

Logical. TRUE indicates that confirmation will be required before a cache directory is set if the default location is not used. FALSE indicates that a cache directory will be set without confirmation. Irrelevant if location = "user". It is recommended to use TRUE. Defaults to TRUE.

Details

Saving outputs to save time running identical models more than once requires files to be saved on your computer. To avoid writing to your computer without your permission, you are required to run this function first to ensure that you know that files are being saved and where files are being saved. The function temporarily sets a hidden environment variable '.cache_env' (with, if empty, assign(".cache_env", new.env(parent = emptyenv()), envir = parent.frame(1)) and with assign("cache_dir", cache_dir, envir = get(".cache_env", envir = parent.frame(1))), if not), which will be removed whenever the environment is cleared.

By default, the function creates the cache directory in the standard place for the operating system being used, appended by ⁠semFromKeys/[projectname]⁠, if a project is being used and the rstudioapi is available. If a project is not being used or the rstudioapi package is not available, then the relative path with be semFromKeys.

In the later case, if two function calls include the same name assignment, then outputs from one will overwrite the other. Therefore, it is recommended to either set the cache directory to something other than the default or use the default within a project with the rstudioapi package installed. Obviously this latter option will likely not work outside of RStudio, so, in that case, it is recommended to use a custom location.

To ensure that R knows what the cache directory is, a hidden environment variable containing the information—.cache_env—is saved within the working environment. Other functions from the package will look for and use .cache_env to identify the cache directory. As a result, whenever the working environment is cleared (e.g., upon closing RStudio) or whenever the .cache_env is otherwise removed, the cache.setup function will need to be re-run before the cache functionality will work, regardless of the status of the any recently used cache directories.

Note also that the function relies on code that was unavailable in versions of R prior to version 4.0, so anyone using an earlier version of R will either have to update R or forgo the caching functionality.

Functions that directly or indirectly might require a cache directory are: cfa.from.keys(), bifactor.from.keys(), efa.from.keys(), esem.from.mods(), and sem.check().

Value

The cache directory path (invisibly). Primarily called to set up the cache configuration.

See Also

cfa.from.keys(), bifactor.from.keys(), efa.from.keys(), esem.from.mods(), and sem.check() for dependent functions; tools::R_user_dir() for default cache directories; here::here() for project-based cache directory setting; and cache.clean() for a function to clean cache.

Examples


  # Setup a cache directory
  cache.setup()
  # Now code with check = TRUE or save_out = TRUE will work, e.g.,
  # Create CFA keys
  keys0 <- c("grit_c", "grit_p", "hope_a", "hope_p")
  keys <- sapply(
    keys0, function(x) names(BFIGritHope)[grep(x, names(BFIGritHope))]
  )
  # Run models
  cfa_fit <- cfa.from.keys(
    keys, BFIGritHope, fit_save = TRUE, check = TRUE, save_out = TRUE
  )
  # Check that models are not run again.
  cfa_fit <- cfa.from.keys(
    keys, BFIGritHope, fit_save = TRUE, check = TRUE, save_out = TRUE
  )


semFromKeys documentation built on July 24, 2026, 5:07 p.m.