How to access a variable stored in a function in R -
one of features of r i've been working lately (thanks r.cache) ability of functions declare other functions. in particular, when 1 this, 1 able have variables inherent part of resulting function.
for example:
functionbuilder <- function(wordtosay) { function() { print(wordtosay) } } can build function so:
functiontorun <- functionbuilder("hello nested world")
then functiontorun() result in "hello nested world". if @ functiontorun (i.e., print it), see code matches functionbuilder. see functiontorun has environment. how can 1 access value of wordtosay stored inside of functiontorun?
at first tried: get("wordtosay",env=functiontorun) ... functiontorun isn't environment , can't transformed environment via as.environment. similarly, because functiontorun isn't environment, can't attach or use with.
i found environment accessor function , set environments, in analgous way how names gets , sets name attributes. therefore, code functiontorun's environment environment(functiontorun) , therefore, can access wordtosay line get("wordtosay",environment(functiontorun)).
Comments
Post a Comment