-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
inject_funs()
more self-contained (#148)
* Move to its own file * Add a test * Make it invisibly return previous values * Polish examples * Only inject once, not once per TLE
- Loading branch information
Showing
4 changed files
with
71 additions
and
40 deletions.
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,54 @@ | ||
#' Inject functions into the environment of `evaluate()` | ||
#' | ||
#' Create functions in the environment specified in the `envir` argument of | ||
#' [evaluate()]. This can be helpful if you want to substitute certain | ||
#' functions when evaluating the code. To make sure it does not wipe out | ||
#' existing functions in the environment, only functions that do not exist in | ||
#' the environment are injected. | ||
#' @param ... Named arguments of functions. If empty, previously injected | ||
#' functions will be emptied. | ||
#' @note For expert use only. Do not use it unless you clearly understand it. | ||
#' @keywords internal | ||
#' @return Invisibly returns previous values. | ||
#' @examples library(evaluate) | ||
#' # normally you cannot capture the output of system | ||
#' evaluate("system('R --version')") | ||
#' | ||
#' # replace the system() function | ||
#' old <- inject_funs(system = function(...) { | ||
#' cat(base::system(..., intern = TRUE), sep = '\n') | ||
#' }) | ||
#' | ||
#' evaluate("system('R --version')") | ||
#' | ||
#' # restore previously injected functions | ||
#' inject_funs(old) | ||
#' @export | ||
inject_funs <- function(...) { | ||
funs <- list(...) | ||
funs <- funs[names(funs) != ''] | ||
old <- .env$inject_funs | ||
.env$inject_funs <- Filter(is.function, funs) | ||
|
||
invisible(old) | ||
} | ||
|
||
local_inject_funs <- function(envir, frame = parent.frame()) { | ||
funs <- .env$inject_funs | ||
if (length(funs) == 0) { | ||
return() | ||
} | ||
|
||
funs_names <- names(funs) | ||
funs_new <- !vapply(funs_names, exists, logical(1), envir, inherits = FALSE) | ||
funs_names <- funs_names[funs_new] | ||
funs <- funs[funs_new] | ||
|
||
defer(rm(list = funs_names, envir = envir), frame = frame) | ||
|
||
for (i in seq_along(funs_names)) { | ||
assign(funs_names[i], funs[[i]], envir) | ||
} | ||
|
||
invisible() | ||
} |
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,7 @@ | ||
test_that("can inject functons into evaluation context", { | ||
old <- inject_funs(f = function() 1) | ||
defer(inject_funs(old)) | ||
|
||
ev <- evaluate("f()") | ||
expect_equal(ev[[2]], "[1] 1\n") | ||
}) |