Skip to content

Commit

Permalink
Implement tar_load_globals()
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Jun 4, 2021
1 parent 05de2f2 commit 460e25e
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export(tar_helper)
export(tar_helper_raw)
export(tar_invalidate)
export(tar_load)
export(tar_load_globals)
export(tar_load_raw)
export(tar_make)
export(tar_make_clustermq)
Expand Down
51 changes: 51 additions & 0 deletions R/tar_load_globals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#' @title Load globals for debugging, testing, and prototyping
#' @export
#' @family debug
#' @description Load user-defined functions, global objects, and
#' settings defined in the target script file (default: `_targets.R`).
#' This function is for debugging, testing, and prototyping only.
#' It is not recommended for use inside a serious pipeline
#' or to report the results of a serious pipeline.
#' @details This function first sources the target script file
#' (default: `_targets.R`)
#' to loads all user-defined functions, global objects, and settings
#' into the current R process. Then, it loads all the packages defined
#' in `tar_option_get("packages")` (default: `(.packages())`)
#' using `library()` with `lib.loc` defined in `tar_option_get("library")`
#' (default: `NULL`).
#' @return `NULL` (invisibly).
#' @inheritParams tar_config_set
#' @param envir Environment to source the target script (default: `_targets.R`).
#' Defaults to the calling environment.
#' @examples
#' if (identical(Sys.getenv("TAR_LONG_EXAMPLES"), "true")) {
#' tar_dir({ # tar_dir() runs code from a temporary directory.
#' tar_script({
#' tar_option_set(packages = "callr")
#' analyze_data <- function(data) {
#' summary(data)
#' }
#' list(
#' tar_target(x, 1 + 1),
#' tar_target(y, 1 + 1)
#' )
#' }, ask = FALSE)
#' tar_load_globals()
#' print(analyze_data)
#' print("callr" %in% (.packages()))
#' })
#' }
tar_load_globals <- function(
envir = parent.frame(),
script = targets::tar_config_get("script")
) {
force(envir)
eval(parse(text = readLines(script)), envir = envir)
map(
x = tar_option_get("packages"),
f = library,
character.only = TRUE,
lib.loc = tar_option_get("library")
)
invisible()
}
2 changes: 1 addition & 1 deletion R/tar_workspace.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ tar_workspace <- function(
build_load_packages(command$packages, command$library)
}
if (source) {
source(script, local = envir)
eval(parse(text = readLines(script)), envir = envir)
}
set.seed(workspace$target$command$seed)
invisible()
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ reference:
- '`tar_make_future`'
- title: Debug
contents:
- '`tar_load_globals`'
- '`tar_traceback`'
- '`tar_workspace`'
- '`tar_workspaces`'
Expand Down
68 changes: 68 additions & 0 deletions man/tar_load_globals.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/tar_traceback.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/tar_workspace.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/tar_workspaces.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/test-tar_load_globals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tar_test("tar_load_globals", {
tar_script({
tar_option_set(packages = "callr")
analyze_data <- function(data) {
summary(data)
}
list(
tar_target(x, 1 + 1),
tar_target(y, 1 + 1)
)
}, ask = FALSE)
envir <- new.env(parent = globalenv())
tar_load_globals(envir = envir)
expect_true(is.function(envir$analyze_data))
expect_true("callr" %in% (.packages()))
})

0 comments on commit 460e25e

Please sign in to comment.