[R] How to modify object's code living in some environment?

Grzegorz Smoliński g@@mo||n@k|1 @end|ng |rom gm@||@com
Mon Dec 27 14:06:49 CET 2021


Hi,

I know it is possible to find the environment in which some object
lives using the 'environment()' function and the name of this object,
but how to modify code of this object after this? Below is MRE:

test <- function() 1

test() # 1

environment(test)$test <- eval(parse(text = "function() 2"))

test() # still 1

.GlobalEnv$test <- eval(parse(text = "function() 3"))

test() # 3

The context is I have shiny app which code I would like to modify (add
something) and the only way to refer to this environment I know is to
use 'environment()' (as I see, those functions do not live in
.GlobalEnv), but as you can see above, I can't use it to modify the
code.

Best regards,

Grzegorz Smoliński



More information about the R-help mailing list