get_globals | R Documentation |
Get global variables on a function
get_globals(fcn, must_exist = FALSE)
fcn |
The |
must_exist |
If TRUE, an error is produced if one of the prospect globals cannot be located. |
A named list of globals.
## Here 'a' is a global variable
a <- 42
f <- function() pi * a
globals <- get_globals(f)
utils::ls.str(globals)
## Here 'a' is not a global variable, because it is part of
## the environment of 'f', which is a local environment
## that comes with function 'f'
f <- local({
a <- 42
function() pi * a
})
globals <- get_globals(f)
utils::ls.str(globals)
## Same here; 'a' is not a global variable
f <- local({
a <- 42
local({
function() pi * a
})
})
globals <- get_globals(f)
utils::ls.str(globals)
my_fcn <- function(prune = FALSE) {
huge <- rnorm(1e6)
n <- 2
g <- local({
pi <- 3.14
function() n * pi
})
globals <- globals::globalsOf(g, envir = environment(g), mustExist = FALSE)
str(globals)
globals <- globals::cleanup(globals)
str(globals)
fcn_globals <- get_globals(g)
str(fcn_globals)
}
my_fcn()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.