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.
Examples
library(evaluate)
# normally you cannot capture the output of system
evaluate("system('R --version')")
#> <evaluation>
#> Source code:
#> system('R --version')
# replace the system() function
old <- inject_funs(system = function(...) {
cat(base::system(..., intern = TRUE), sep = "\n")
})
evaluate("system('R --version')")
#> <evaluation>
#> Source code:
#> system('R --version')
#> Text output:
#> R version 4.4.1 (2024-06-14) -- "Race for Your Life"
#> Copyright (C) 2024 The R Foundation for Statistical Computing
#> Platform: x86_64-pc-linux-gnu
#>
#> R is free software and comes with ABSOLUTELY NO WARRANTY.
#> You are welcome to redistribute it under the terms of the
#> GNU General Public License versions 2 or 3.
#> For more information about these matters see
#> https://www.gnu.org/licenses/.
#>
# restore previously injected functions
inject_funs(old)