[R] Substituting a user-defined function for a standard function in graphics

Duncan Murdoch murdoch at stats.uwo.ca
Thu Jul 16 00:20:37 CEST 2009


On 15/07/2009 4:25 PM, John Kolassa wrote:
> Dear R-help readers,
>      I'm trying to substitute my own version of plot.default, in order to
> capture low-level graphics input for further manipulation.  I seem to be
> having difficulties getting the versions of functions that I intend to use
> called at the right places. 
>      Specifically, I have an object produced by survfit from the survival
> package, and I want to save the values that plot.survfit would normally
> send to plot.default from the graphics package.  I've tried the following:

That's probably hard, but it is very easy to get a copy of plot.survfit 
from the survival package source code, and modify it to your heart's 
content.

If you are doing something that would be useful to others, it might be a 
good idea to contact the maintainer of the survival package to ask him 
to incorporate your code, but you'd better have pretty high quality code 
before you ask for that.

Duncan Murdoch


> 1. I put my version of plot.default into .GlobalEnv.  This works, in the
> sense of running without an error, but doesn't do what I want, since my
> version of plot.default never gets called, apparently because since the
> version of plot comes from graphics, the graphics namespace takes
> precedence over .GlobalEnv.
> 
> 2. I put both my version of plot.default, and a copy of plot from graphics
> (with a browser call on top, so that I know it's my version), into
> .GlobalEnv. In this case, I get an error, apparently because my version of
> plot hits the line UseMethod("plot"), can't see the survfit method, and so
> the fit gets dumped to plot.default, plot.default can only handle
> arguments as defined in the graphics documentation on xy.coordinates, and
> survfit isn't in this format.  I can't figure out why UseMethod("plot")
> doesn't send the survfit object to plot.survfit.  methods("plot") gives
> survfit with an asterisk (denoting invisibility); is the invisibility a
> problem?
> 
> 3. I made a package incorporating the graphics plot and my plot.default,
> without a namespace file, and loaded it via a library call.  This again
> gave me an error, since the survfit object seems to be sent directly to
> plot.default rather than plot.survfit.
> 
> 4. I made a package incorporating the graphics plot and my plot.default,
> this time with a namesapce file that exports plot and plot.default. This
> again gave me the same error, since the survfit object seems to be sent
> directly to plot.default rather than plot.survfit.
> 
> I read the document Writing R Extensions, but wasn't able to solve this
> problem.  Any pointers would be greatly appreciated.
> 
> Thanks, John Kolassa
> 
> ______________________________________________
> R-help at 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.




More information about the R-help mailing list