John Kolassa
2009-Jul-15 20:25 UTC
[R] Substituting a user-defined function for a standard function in graphics
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: 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
David Winsemius
2009-Jul-15 21:04 UTC
[R] Substituting a user-defined function for a standard function in graphics
On Jul 15, 2009, at 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.So wouldn't you instead create a modified plot.survfit? Looking at the plot.survfit code, it appears that the only time plot is called is to set up the axes (i.e. with ,type="n", )and then other functions are used to draw the curves. <snipped recounting of unsuccessful efforts> David Winsemius, MD Heritage Laboratories West Hartford, CT
Duncan Murdoch
2009-Jul-15 22:20 UTC
[R] Substituting a user-defined function for a standard function in graphics
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.