如何用点 (...) 参数覆盖硬编码的函数参数?

Mik*_*kko 5 r

我正在尝试创建一个具有一组默认值的绘图函数,并且可以通过使用该函数plot在点 ( ...) 参数内接受的任何参数来灵活地更改这些值。一个例子:

PlotIt <- function(x, y, ...) {
  plot(x, y, type = "l", asp = 1, ...)
}

x <- 1:10
y <- 10:1

PlotIt(x = x, y = y)
# Returns a plot
PlotIt(x = x, y = y, asp = NA)
# Error in plot.default(x, y, type = "l", asp = 1, ...) : 
#  formal argument "asp" matched by multiple actual arguments
Run Code Online (Sandbox Code Playgroud)

该错误自然是因为我尝试将 asp 参数两次传递到plot. 到目前为止,我最好的笨拙尝试是做出一个 if-else 语句来考虑到这一点(该方法是从此处修改的):

PlotIt2 <- function(x, y, ...) {

  mc <- match.call(expand.dots = FALSE)

  if(names(mc$...) %in% "asp") {
  plot(x, y, type = "l", ...)  
  } else {
  plot(x, y, type = "l", asp = 1, ...)  
  }

}

PlotIt2(x = x, y = y, asp = NA)
# works
Run Code Online (Sandbox Code Playgroud)

要对可以通过参数设置的所有可能参数执行此操作...,我需要编写一个很长的 if-else 语句。有没有更优雅的方法来做到这一点?

这个问题与此相关,不同之处在于我想自动覆盖参数设置的所有参数...

Ale*_*xis 4

如果您只想使用基本 R,您可以将所有内容放入列表中,并根据参数名称删除重复项(确保默认值位于最后,以便在出现在 中时将其删除...):

PlotIt <- function(x, y, ...) {
  arguments <- list(
    x = x,
    y = y,
    ...,
    type = "l",
    asp = 1
  )

  arguments <- arguments[!duplicated(names(arguments))]

  do.call("plot", arguments)
}
Run Code Online (Sandbox Code Playgroud)

如果您不介意依赖rlang,您还可以执行以下操作,使用.homonyms来获得相同的功能(并检查绘图的轴标签,基本 R 和rlang版本之间会有所不同):

PlotIt <- function(x, y, ...) {
  require("rlang")
  arguments <- rlang::dots_list(
    rlang::expr(x),
    rlang::expr(y),
    ...,
    type = "l",
    asp = 1,
    .homonyms = "first"
  )

  call <- rlang::call2("plot", !!!arguments)
  eval(call)
}
Run Code Online (Sandbox Code Playgroud)