Skip to content

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.

Usage

inject_funs(...)

Arguments

...

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.

Examples

library(evaluate)
# normally you cannot capture the output of system
evaluate("system('R --version')")
#> [[1]]
#> $src
#> [1] "system('R --version')"
#> 
#> attr(,"class")
#> [1] "source"
#> 

# replace the system() function
inject_funs(system = function(...) cat(base::system(..., intern = TRUE), sep = '\n'))

evaluate("system('R --version')")
#> [[1]]
#> $src
#> [1] "system('R --version')"
#> 
#> attr(,"class")
#> [1] "source"
#> 
#> [[2]]
#> [1] "R version 4.4.0 (2024-04-24) -- \"Puppy Cup\"\nCopyright (C) 2024 The R Foundation for Statistical Computing\nPlatform: x86_64-pc-linux-gnu\n\nR is free software and comes with ABSOLUTELY NO WARRANTY.\nYou are welcome to redistribute it under the terms of the\nGNU General Public License versions 2 or 3.\nFor more information about these matters see\nhttps://www.gnu.org/licenses/.\n\n"
#> 

inject_funs()  # empty previously injected functions