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

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Mon Dec 27 16:51:05 CET 2021


I think ?body, especially the replacement form body<-, might be what
you are looking for.

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Mon, Dec 27, 2021 at 5:07 AM Grzegorz Smoliński
<g.smolinski1 using gmail.com> wrote:
>
> 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
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list