如何在R中获取点 - 点参数的名称

use*_*313 3 r ellipsis

如何获取包含传递给函数的点 - 点参数名称的字符向量,例如:

test<-function(x,y,...)
{
    varnames=deparseName(substitute(list(...)))
    # deparseName does not exist, this is what I want !
    # so that I could *for example* call:

    for(elt in varnames)
       {print(varnames);}
}

v1=11
v2=10
test(12,12,v1,v2)

## would print 
#v1
#v2
Run Code Online (Sandbox Code Playgroud)

Jua*_*íaz 8

试试这个:

test<-function(x,y,...)
{
  mc <- match.call(expand.dots = FALSE)
  mc$...
}

v1=11
v2=10
test(12,12,v1,v2)
[[1]]
v1

[[2]]
v2
Run Code Online (Sandbox Code Playgroud)


ali*_*ire 6

为了稍微扩展其他答案,如果您只想将参数传递给...名称,则可以is.name在将它们解析为字符串之前对未计算的点进行子集化:

v1 <- 12
v2 <- 47
v3 <- "foo"

test <- function(x, y, ...){
    dots <- match.call(expand.dots = FALSE)$...
    dots <- dots[sapply(dots, is.name)]
    sapply(dots, deparse)
}

test(2, y = v1, z = v2, 1, v3)
#>    z      
#> "v2" "v3"
Run Code Online (Sandbox Code Playgroud)


Cat*_*ath 5

您可以使用deparsesubstitute获取所需的信息(另请参见此Q&A):

test<-function(x, y, ...)
{
    varnames=lapply(substitute(list(...))[-1], deparse)
    lapply(varnames, print)
    return(invisible())
}

test(12,12,v1,v2)
#[1] "v1"
#[1] "v2"
Run Code Online (Sandbox Code Playgroud)