Request: An additional hook in plot.new() that is called prior to the call to .Internal(plot.new()). Reason: To allow the hook to set up or modify a graphics device that the new plot will appear in. The code change needed for this is simple - just 4 new lines of R code in src/library/graphics/R/plot.R:plot.new() Current definition of plot.new() in src/library/graphics/R/plot.R: plot.new <- function() { .Internal(plot.new()) for(fun in getHook("plot.new")) { if(is.character(fun)) fun <- get(fun) try(fun()) } invisible() } New definition of plot.new() in src/library/graphics/R/plot.R: plot.new <- function() { for(fun in getHook("plot.prenew")) { if(is.character(fun)) fun <- get(fun) try(fun()) } .Internal(plot.new()) for(fun in getHook("plot.new")) { if(is.character(fun)) fun <- get(fun) try(fun()) } invisible() } In src/library/graphics/man/frame.Rd after the existing sentence beginning "There is a hook..." in the DETAILS section, the following sentence could be added: "There is another hook called \code{"plot.prenew"} which is called before advancing the frame. This hook can be used to create a new plot " The name of the hook is not very important -- I've suggested "plot.prenew" here. Another possibility could be "plot.new0". More detail on the reason: In a tabbed graphics widget (https://r-forge.r-project.org/projects/tabbedplots/ ), having this hook would enable it to operate in a mode where a new tab is automatically created for each new plot. thanks for your consideration, Tony Plate
Thomas Friedrichsmeier
2011-Jan-23 19:06 UTC
[Rd] feature request: additional hook in plot.new()
Hi Tony, On Sunday 23 January 2011, you wrote:> Request: An additional hook in plot.new() that is called prior to the call > to .Internal(plot.new()). Reason: To allow the hook to set up or modify a > graphics device that the new plot will appear in.for what it's worth, you can work around the lack of this hook by modifying plot.new(). Here's what we do in RKWard (in order to add a plot history feature): "plot.new" <- function () { # [your code goes here] eval (body (.rk.plot.new.default)) } formals (plot.new) <- formals (graphics::plot.new) .rk.plot.new.default <- graphics::plot.new # Note: This needs to be called *after* the package has been loaded assignInNamespace ("plot.new", plot.new, envir=as.environment ("package:graphics")) Note that at least persp() also sets up a new plot, _without_ calling plot.new(). So you'll want to catch that as well. In RKWard we use this technique at a number of places to insert "hooks" where there are none, regularly. Regards Thomas -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: This is a digitally signed message part. URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20110123/bc417e9d/attachment.bin>
On Jan 23, 2011, at 10:44 AM, Tony Plate wrote:> Request: An additional hook in plot.new() that is called prior to the call to .Internal(plot.new()). > Reason: To allow the hook to set up or modify a graphics device that the new plot will appear in. > > The code change needed for this is simple - just 4 new lines of R code in src/library/graphics/R/plot.R:plot.new() > > Current definition of plot.new() in src/library/graphics/R/plot.R: > > plot.new <- function() > { > .Internal(plot.new()) > for(fun in getHook("plot.new")) { > if(is.character(fun)) fun <- get(fun) > try(fun()) > } > invisible() > } > > New definition of plot.new() in src/library/graphics/R/plot.R: > > plot.new <- function() > { > for(fun in getHook("plot.prenew")) { > if(is.character(fun)) fun <- get(fun) > try(fun()) > } > .Internal(plot.new()) > for(fun in getHook("plot.new")) { > if(is.character(fun)) fun <- get(fun) > try(fun()) > } > invisible() > } > > In src/library/graphics/man/frame.Rd after the existing sentence beginning "There is a hook..." in the DETAILS section, the following sentence could be added: > > "There is another hook called \code{"plot.prenew"} which is called before advancing the frame. This hook can be used to create a new plot " > > The name of the hook is not very important -- I've suggested "plot.prenew" here. Another possibility could be "plot.new0". > > More detail on the reason: > > In a tabbed graphics widget (https://r-forge.r-project.org/projects/tabbedplots/ ), having this hook would enable it to operate in a mode where a new tab is automatically created for each new plot. >Just note on the latter: I would expect this to be usually better handled by the device/UI as you will be in conflict with things like plot history, already supported tabbed devices etc. Do you have a use case where the above has any benefits over separate devices in tabs? (The website has little detail so I'm just guessing what you're up to ..). Cheers, Simon