[英]Defining default R function parameters on processed arguments
I'm writing a package function that needs defaults parameters to work, but whose values have to be taken from processed (other) arguments of the function. 我正在编写一个需要默认参数才能工作的包函数,但其值必须取自函数的已处理(其他)参数。 As of now, I'm doing it like so :
截至目前,我这样做:
myfunc <- function(x, y, n=NULL){
processed <- some_other_func(x,y)
x <- processed$x
y <- processed$y
if(is.null(n)){
n <- length(intersect(x,y))/3
}
# do stuff
}
But ideally, I'd like a default expression to be put instead of NULL
because it seems inconsistent if my doc says that the default is length(intersect(x,y))/3
. 但理想情况下,我想要一个默认表达式而不是
NULL
因为如果我的doc说默认值是length(intersect(x,y))/3
它似乎不一致。
Do you know of a way I could specify the default parameter to be more comprehensible ? 你知道我可以指定默认参数的方式更容易理解吗?
Maybe you could solve it something like this? 也许你可以解决这个问题吗?
default_function <- function(v1, v2)
{
return((v1 + v2)/2)
}
dummy_function <- function(x, y, n = default_function)
{
x = x
y = y
if(class(n) == "function") n = n(x,y) # if n is any kind of function do it
print(paste(x, y, n))
}
dummy_function(1, 2)
dummy_function(1, 2, 100)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.