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

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -