我有一个主函数,它将参数传递给另一个rpart
用于生成模型的函数。我希望有可能rpart.control
使用省略号从主函数中指定。如果没有定义cp
or minbucket
,那么我想对这两个参数使用我的默认值。
到目前为止,我还没有成功 - 请参阅下面的草稿。目前该功能抱怨没有cp
找到。我理解为什么,但无法弄清楚如果用户没有提供自己的控件,如何应用默认值。
require(rpart)
a <- function(df, ...) {
b(df, ...)
}
b <- function(df, ...) {
if (is.null(cp)) cp <- 0.001
if (is.null(minbucket)) minbucket = nrow(df) / 20
rcontrol <- rpart.control(cp = cp, minbucket = minbucket, ...)
rcontrol
}
a(iris) # no controls set my defaults for cp and minbucket
a(iris, cp = 0.123) # set cp, use my default for minbucket
a(iris, cp = 0.123, minbucket = 100) # set cp, and minbucket
a(iris, maxcompete = 3) # use my defaults for cp and minbucket, but pass maxcompete