How does R dispatch plot functions? The standard generic is defined as
plot <- function (x, y, ...) UseMethod("plot")
So usually, all plot methods need arguments x and y. Yet, there exists a variety of other functions with different arguments. Some functions only have argument x:
plot.data.frame <- function (x, ...)
others even have neither x nor y:
plot.formula <- function(formula, data = parent.frame(), ..., subset,
ylab = varnames[response], ask = dev.interactive())
How does this work and where is this documented?
Background
In my package papeR (see GitHub) I want to replace the function plot.data.frame, which is defined in the R package graphics with my own version. Yet, this is usually not allowed
Do not replace registered S3 methods from base/recommended packages, something which is not allowed by the CRAN policies and will mean that everyone gets your method even if your namespace is unloaded.
as Brian Ripley let me know last time I tried to do such a thing. A possible solution is as follows:
If you want to change the behaviour of a generic, say
predict(), for an existing class or two, you could add such as generic in your own package with default methodstats::predict, and then register modified methods for your generic (in your own package).
For other methods I could easily implement this (e.g. toLatex), yet, with plot I am having problems. I added the following to my code:
## overwrite standard generic
plot <- function(x, y, ...)
UseMethod("plot")
## per default fall back to standard generic
plot.default <- function(x, y, ...)
graphics::plot(x, y, ...)
## now specify modified plot function for data frames
plot.data.frame <- function(x, variables = names(x), ...)
This works for data frames and plots with x and y. Yet, it does not work if I try to plot a formula etc:
Error in eval(expr, envir, enclos) :
argument "y" is missing, with no default
I also tried to use
plot.default <- function(x, y, ...)
UseMethod("graphics::plot")
but then I get
Error in UseMethod("graphics::plot") :
no applicable method for 'graphics::plot' applied to an object of class "formula"
So the follow up question is how I can fix this?
[Edit:] Using my solution below fixes the problems within the package. Yet, plot.formula is broken afterwards:
library("devtools")
install_github("hofnerb/papeR")
example(plot.formula, package="graphics") ## still works
library("papeR")
example(plot, package = "papeR") ## works
### BUT
example(plot.formula, package="graphics") ## is broken now
plotandplot.default? Just usingplot.data.frame <- function(...) {print("Cheese")}; plot(data.frame(1))gives me the "expeced behavior". Anyway, interesting question … - CL.plot.formulahas got a mandatory second argument (thedata). That it isn't calledyis irrelevant since argument matching is done by position. Consider this:plot.myclass <- function(a, b, ...) print(b); x <- 1; y <- 2; class(x) <- "myclass"; plot(x, y)However, I beleive the generic can be called with less than two arguments because of the special evaluation done byUseMethod. See the language definition section 5.4. - Rolandno applicable method for 'graphics::plot') looks like I cannot findplot.formulaas it isn't exported? - Benjamin HofnerUseMethodmust name a function andgraphics::plotis not a function name. Rather, it is a call (to function::). Doesn'tplot.default <- function(...) graphics::plot(...)work for you? (I don't want to build a package for testing right now.) - Roland