【发布时间】:2019-12-21 19:18:33
【问题描述】:
我正在尝试构建一个通用函数,该函数根据所选算法的类型分派给其他函数。在下面的示例中,算法类型仅由“algo1”、“algo2”等字符选择... x 是类 S4 的对象。参数 A 和 B 对所有方法(函数)都是通用的。我想将函数命名为 Myfun.algo1 和 Myfun.algo2。这些函数有一些参数设置为一些默认值,因此有一个泛型的目的。
#' @export Myfun
Myfun<-function(x, type = c("algo1", "algo2"), A, B, ...){
switch(type,
algo1={res<-do.call(Myfun.algo1, list(x, A, B, ...))},
algo2={res<-do.call(Myfun.algo2, list(x, A, B, ...))},
stop("Unknown Type"))
return(res)
}
#' @rdname MyFun
#' @export
MyFun.algo1<-function(x, A, B, C=2, D=300){
#Do a bit of magic.
}
#' @rdname MyFun
#' @export
MyFun.algo2<-function(x, A, B, E=10, F=1000){
#Do more magic.
}
它可以工作,但是当我检查包时(使用 check()),我一直有同样的错误:
checking S3 generic/method consistency ... WARNING MyFun: function(x, type, A, B, ...) MyFun.algo1: function(x, A, B, C, D) #Same thing for algo2. See section 'Generic functions and methods' in the 'Writing R Extensions' manual. Found the following apparent S3 methods exported but not registered: MyFun.algo1 MyFun.algo2 See section 'Registering S3 methods' in the 'Writing R Extensions' manual.
我查看了手册,但它确实没有帮助。我尝试通过添加@method 和@S3method 来更改roxygen2 标签,但没有帮助。我尝试更改 ... 的顺序并将“类型”放在 MyFun 的末尾,它也没有帮助。我不明白......我做错了什么?为什么我不允许这样做?有没有办法解决这个问题?
【问题讨论】: