R/tar_dir.R

Defines functions tar_dir

Documented in tar_dir

#' @title Execute code in a temporary directory.
#' @export
#' @keywords internal
#' @description Not a user-side function. Just for CRAN.
#' @details Runs code inside a new `tempfile()` directory
#'   in order to avoid writing to the user's file space.
#'   Used in examples and tests in order to comply with CRAN policies.
#' @return Return value of the user-defined code.
#' @param code User-defined code.
#' @examples
#' tar_dir(file.create("only_exists_in_tar_dir"))
#' file.exists("only_exists_in_tar_dir")
tar_dir <- function(code) {
  code <- substitute(code)
  dir <- tempfile(pattern = "targets_")
  dir_create(dir)
  old <- eval(parse(text = "setwd(dir)", keep.source = FALSE))
  on.exit({
    eval(parse(text = "setwd(old)", keep.source = FALSE))
    unlink(dir, recursive = TRUE)
  })
  eval(code, envir = parent.frame())
}

Try the targets package in your browser

Any scripts or data that you put into this service are public.

targets documentation built on Oct. 3, 2024, 1:11 a.m.