Skip to contents

Run all the R scripts in a directory in the environment specified.

Usage

tar_source(
  files = "R",
  envir = targets::tar_option_get("envir"),
  change_directory = FALSE
)

Arguments

files

Character vector of file and directory paths to look for R scripts to run. Paths must either be absolute paths or must be relative to the current working directory just before the function call.

envir

Environment to run the scripts. Defaults to tar_option_get("envir"), the environment of the pipeline.

change_directory

Logical, whether to temporarily change the working directory to the directory of each R script before running it.

Value

NULL (invisibly)

Details

tar_source() is a convenient way to load R scripts in _targets.R to make custom functions available to the pipeline. tar_source() recursively looks for files ending in .R or .r, and it runs each with eval(parse(text = readLines(script_file, warn = FALSE)), envir).

Storage access

Several functions like tar_make(), tar_read(), tar_load(), tar_meta(), and tar_progress() read or modify the local data store of the pipeline. The local data store is in flux while a pipeline is running, and depending on how distributed computing or cloud computing is set up, not all targets can even reach it. So please do not call these functions from inside a target as part of a running pipeline. The only exception is literate programming target factories in the tarchetypes package such as tar_render() and tar_quarto().

Examples

if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) { # for CRAN
tar_dir({ # tar_dir() runs code from a temp dir for CRAN.
# Running in tar_dir(), these files are written in tempdir().
dir.create("R")
writeLines("f <- function(x) x + 1", file.path("R", "functions.R"))
tar_script({
  tar_source()
  list(tar_target(x, f(1)))
})
tar_make()
tar_read(x) # 2
})
}