0

Is there a way, to change the execution environment of a non-exported function defined in package's namespace?

My goal is to swiftly force this function to follow different scoping path to invoke some custom methods/objects (defined in the new environment), without the need of duplicating the whole package. Currently, I'm unsuccessful with

> environment(ggplot2:::create_layout)
<environment: namespace:ggplot2>
> env <- new.env()
> parent.env(env) <- asNamespace('ggplot2')
> environment(ggplot2:::create_layout) <- new.env
Error in environment(ggplot2:::create_layout) <- new.env : 
  object 'ggplot2' not found

In this specific example i would like create_layout to use a modified version of ggplot2's ggproto function and Layer object present in the env environment. My logic was that changing the searching scope is less messy, than modifying the object's body inside the namespace.

mjktfw
  • 840
  • 6
  • 14
  • Package environments are generally locked so you can't reassign a new value to an existing symbol in a namespace (which is what happens when you try to change a functions environment). Can you give a more specific example of a behavior you are trying to change? – MrFlick Jun 23 '17 at 21:05
  • I guess it's just a duplicate of other questions concerning `assignInNamespace`, since the solution can be achieved with creating a local copy of `create_layout`, changing it's environment with `environment<-`, and then calling `assignInNamespace('create_layout', create_layout, ns = 'ggplot2')`. I've also edited my post with description of what I want to achieve. – mjktfw Jun 23 '17 at 21:17
  • https://stackoverflow.com/questions/38388570/how-to-modify-unexported-object-in-a-package – Hong Ooi Jun 23 '17 at 21:21

0 Answers0