CRAN策略不允许替换在基本或推荐包中定义的单个方法(对于通用函数)。他们建议包作者替换标准泛型并使用xxx.default
方法,然后调用原始标准泛型。
我的包 papeR 的示例如下:
## 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), ...)
完整的代码可以在GitHub找到。
对于toLatex
这样的各种泛型,这种方法非常有效。然而,上述绘图方法定义打破了标准plot.formula
函数:
library("devtools")
install_github("hofnerb/papeR")
## still works:
example(plot.formula, package="graphics")
### But:
library("papeR")
example(plot.formula, package="graphics")
## Error in eval(expr, envir, enclos) : object 'Ozone' not found
我仍然可以使用
graphics:::plot.formula(Ozone ~ Wind, data = airquality)
虽然。其他绘图功能继续工作,参见例如。
example("plot", package = "graphics")
其他信息
在NAMESPACE我有
importFrom("graphics", "plot", "plot.default", ...)
export(plot, plot.data.frame, ...)
S3method(plot, default)
S3method(plot, data.frame)
有关此主题的上一次讨论(重点不同),另请参阅here。
答案 0 :(得分:0)
有关讨论和解决方案(定义{{1}}并使用new_class
),请参阅
https://stat.ethz.ch/pipermail/r-package-devel/2015q3/000463.html