-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05de2f2
commit 460e25e
Showing
9 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
}) |