R:如何返回`fn(…)`中的`…`的确切形式而不计算`…`?

R:如何返回`fn(…)`中的`…`的确切形式而不计算`…`?,r,nse,R,Nse,考虑一下这个代码 fn = function(...) { # print the content of ... without evaluation? } 我希望fn(a=b)的输出是“a=b”和fn(a=gn(b),3~b~a,dd,e=2+f,h=hn(jn(cdf))成为列表(“a=gn(b)”,“3~b~a”,“dd”,“e=2+f”,“h=hn(jn(cdf))” 我似乎找不到合适的NSE函数。我更喜欢Base R,因此我更了解流程。我最接近的是这个 fn = function

考虑一下这个代码

fn = function(...) {
  # print the content of ... without evaluation?
}
我希望
fn(a=b)
的输出是
“a=b”
fn(a=gn(b),3~b~a,dd,e=2+f,h=hn(jn(cdf))
成为
列表(“a=gn(b)”,“3~b~a”,“dd”,“e=2+f”,“h=hn(jn(cdf))”

我似乎找不到合适的NSE函数。我更喜欢Base R,因此我更了解流程。我最接近的是这个

fn = function(...) {
   res = rlang::enexprs(...)
   paste0(names(res), ifelse(names(res)=="",names(res) , "=") , sapply(res, capture.output))
}

您可以使用
match.call

fn = function(...) {
  temp <- as.list(match.call())[-1]
  as.list(sub('^=', '', paste0(names(temp), '=', temp)))
}

fn(a = b)
#[[1]]
#[1] "a=b"

fn(a = gn(b), 3~b~a, dd, e = 2 + f, h = hn(jn(cdf)))
#[[1]]
#[1] "a=gn(b)"

#[[2]]
#[1] "3 ~ b ~ a"

#[[3]]
#[1] "dd"

#[[4]]
#[1] "e=2 + f"

#[[5]]
#[1] "h=hn(jn(cdf))"
fn=函数(…){

tempmatch.call
()的替代方法是未记录的
..()

fn
fn <- function(...) {
  x <- substitute(...())
  y <- ifelse(nchar(names(x)) > 0, paste(names(x), "=", x), as.character(x))
  as.list(y)
}

fn(a = b)
fn(a = gn(b), 3~b~a, dd, e = 2 + f, h = hn(jn(cdf)))