John Kolassa
2012-Dec-11 19:32 UTC
[R] Reassign functions called by other functions in package graphics
I am trying to get a function written in R that calls a cascade of functions from the graphics package, and I want to eventually call replacements to functions in the graphics package instead of the originals. Specifically, I have a function that calls qqnorm in stats, which calls qqnorm.default in stats, which calls plot in graphics, which calls plot.default and plot.new, and I want my own functions plot.default and plot.new that I entered at the command line (which I guess means that it is in .GlobalEnv). I'd like my plot.default and plot.new called (this afternoon, at least) in place of every invocation of plot.default and plot.new resp.. So far I've tried: reassignInPackage from R.utils: reassignInPackage("plot.default","graphics",my.plot.default) gives "Error in assignInNamespaceT(name, value, ns = pkgName, envir = env) : locked binding of ?plot.default? cannot be changed", even after unlockBinding("plot.default",as.environment("package:graphics")) assignInNamespace seems to almost work. Here's what I enter: plot.default<-function(...){cat("Entered plot.default")} plot.new<-function(...){cat("Entered plot.new")} assignInNamespace("plot.default",plot.default,ns="graphics") assignInNamespace("plot.new",plot.new,ns="graphics") qqnorm(1:10) and here's what I get: Entered plot.newError in plot.xy(xy, type, ...) : plot.new has not been called yet It seems as though my new plot.new is being called, but the existing plot.default is being called, even though the new version is in graphics::plot.default. I conjecture that this is because the original plot.default got exported when the package loaded, but is there a way to overwrite this? Thanks, John
Greg Snow
2012-Dec-12 04:08 UTC
[R] Reassign functions called by other functions in package graphics
I think the error is because the other functions are expecting the plot.new function to do some specific things to set up the graphics device for a new plot, but your function did not do those things. You might consider the trace function as an alternative to what you are trying. It can be used to insert additional code into existing functions, or replace them altogether. On Tue, Dec 11, 2012 at 12:32 PM, John Kolassa <kolassa@stat.rutgers.edu>wrote:> I am trying to get a function written in R that calls a cascade of > functions from the graphics package, and I want to eventually call > replacements to functions in the graphics package instead of the originals. > Specifically, I have a function that calls qqnorm in stats, which calls > qqnorm.default in stats, which calls plot in graphics, which calls > plot.default and plot.new, and I want my own functions plot.default and > plot.new that I entered at the command line (which I guess means that it is > in .GlobalEnv). I'd like my plot.default and plot.new called (this > afternoon, at least) in place of every invocation of plot.default and > plot.new resp.. So far I've tried: > > reassignInPackage from R.utils: > reassignInPackage("plot.default","graphics",my.plot.default) gives "Error > in assignInNamespaceT(name, value, ns = pkgName, envir = env) : locked > binding of ‘plot.default’ cannot be changed", even after > unlockBinding("plot.default",as.environment("package:graphics")) > > assignInNamespace seems to almost work. Here's what I enter: > > plot.default<-function(...){cat("Entered plot.default")} > plot.new<-function(...){cat("Entered plot.new")} > assignInNamespace("plot.default",plot.default,ns="graphics") > assignInNamespace("plot.new",plot.new,ns="graphics") > qqnorm(1:10) > > and here's what I get: > > Entered plot.newError in plot.xy(xy, type, ...) : plot.new has not been > called yet > > It seems as though my new plot.new is being called, but the existing > plot.default is being called, even though the new version is in > graphics::plot.default. I conjecture that this is because the original > plot.default got exported when the package loaded, but is there a > way to overwrite this? Thanks, John > > ______________________________________________ > R-help@r-project.org mailing list > 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. >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]
Maybe Matching Threads
- assignInNamespace to create a setwd() replacement: how to use unlockBinding()?
- assignInNamespace and new bindings
- odd assignInNamespace / setGeneric interaction
- odd assignInNamespace / setGeneric interaction
- Bug report: R.home() cause package Rcpp failed executing sourceCpp, similar bug are labeled "BUG 16660" since 2016 and here I could provide a solution that tested in my laptop.