在R中使用多个三点省略号

在R中使用多个三点省略号,r,R,有没有一种方法可以将任意参数传递给函数中的多个命令?下面的函数显然不起作用,但我希望它能解释我试图实现的目标 test = function(x = rnorm(20), y = rnorm(20), ..., ---){ plot(x, y, type = "p", ...) lines(x, y, ---) } 目标是能够编写一个函数,用线和点和多边形创建绘图,并且可以为每个命令获取任意参数并将其传递给相应的命令,而无需我显式指定每个命令的参数。这里是一种不成熟的方法:

有没有一种方法可以将任意参数传递给函数中的多个命令?下面的函数显然不起作用,但我希望它能解释我试图实现的目标

test = function(x = rnorm(20), y = rnorm(20), ..., ---){
    plot(x, y, type = "p", ...)
    lines(x, y, ---) 
}

目标是能够编写一个函数,用
线
多边形
创建绘图,并且可以为每个命令获取任意参数并将其传递给相应的命令,而无需我显式指定每个命令的参数。

这里是一种不成熟的方法:

.. <- "/////" #or anything which won't be used as a valid parameter

f <- function(...){
  arguments <- list(...)
  if(.. %in% arguments){
    i <- which(arguments == ..)
    terms <- unlist(arguments[1:(i-1)])
    factors <- unlist(arguments[(i+1):length(arguments)])
    c(sum(terms),prod(factors))
  }
}
显然,您可以将此想法扩展到多个
字段,每个字段用
分隔。

选项1 功能

test = function(x = rnorm(20), y = rnorm(20), plot_options = NA, ...){
    if (is.na(plot_options) == FALSE){
        eval(parse(text = paste0("plot(x, y, ", plot_options, ")")))
    } else {
        plot(x, y, type = "n")
    }
    lines(x, y, ...) 
}
test2 = function(x = rnorm(20), y = rnorm(20), ..., line_options){
    plot(x, y, ...)
    if (missing(line_options)) { 
        lines(x, y) 
    } else { 
        do.call(lines, c(list(x = x, y = y), line_options))
    }
}
用法

test()
par(mfrow = c(2, 2), mar = c(2, 2, 1, 1))
test2(main = 'default')
test2(line_options = list(lty = 2), main = 'line')
test2(col = 'red', main = 'plot')
test2(col = 'red', line_options = list(lty = 2, col = 'blue'), main = 'line and plot')

选项2(“s解决方案”) 功能

test = function(x = rnorm(20), y = rnorm(20), plot_options = NA, ...){
    if (is.na(plot_options) == FALSE){
        eval(parse(text = paste0("plot(x, y, ", plot_options, ")")))
    } else {
        plot(x, y, type = "n")
    }
    lines(x, y, ...) 
}
test2 = function(x = rnorm(20), y = rnorm(20), ..., line_options){
    plot(x, y, ...)
    if (missing(line_options)) { 
        lines(x, y) 
    } else { 
        do.call(lines, c(list(x = x, y = y), line_options))
    }
}
用法

test()
par(mfrow = c(2, 2), mar = c(2, 2, 1, 1))
test2(main = 'default')
test2(line_options = list(lty = 2), main = 'line')
test2(col = 'red', main = 'plot')
test2(col = 'red', line_options = list(lty = 2, col = 'blue'), main = 'line and plot')

也许你可以解释一下这个函数的用途。R如何知道要传递给哪个函数的参数?现在还不清楚你会怎么想。这也可能是一个重复:我认为你需要详细阐述,可能需要一个比你在最后一段更具体的例子。我很确定你只需要做一个包含“选项”的参数,然后把它们解析出来。例如,请参见
optim
control
参数。没有什么是不可能的*。如果您能更全面地描述所需的行为,这将非常有用。显示您计划如何调用此函数。期望的输出是什么。(*一些限制适用)我的意思是你需要展示一个函数如何使用的示例(可能有几个示例,因为你正在尝试灵活的语法)以及预期的输出。关闭时,我正在研究另一个答案(尽管我认为重复是好的)。您的
eval
带有一系列参数似乎是一种糟糕的做法
do.call
列表一起使用
要好得多(就像在重复中一样)。不幸的是,
plot
使用了
substitute
作为默认轴名称,这与
do.call不兼容。一个不错的解决方法是使用
..
调用
plot()
行选项列表
调用
。试试这个:
test2=函数(x=rnorm(20),y=rnorm(20),…,行选项){plot(x,y,…;if(缺少行选项)){line(x,y)}否则{do.调用(行,c(list)(x=x,y=y)用法示例:
par(mfrow=c(2,2),mar=c(2,2,1));test2(main='default');test2(line_options=list(lty=2),main=line);test2(col='red',main='plot');test2(col='red',line_options=list(lty=2,col='blue'),main='line and plot')这样看起来更干净。