简体   繁体   English

R非标准评估 - 传递清单

[英]R non-standard evaluation - passing a list

How can I pass a character vector using NSE: 如何使用NSE传递字符向量:

fun <- function(x){
  x_  <-deparse(substitute(x))
  print(x_)
}

fun_ <- function(x){
 do_something(x)
}

For example, 例如,

fun(x= a, b, c)

should print interpret the argument to x as a vector c(a, b, c) and pass a character vector to fun_(x) : 应该打印将x的参数解释为向量c(a,b,c)并将字符向量传递给fun_(x)

fun_(x=c("a", "b", "c"))    

There are additional parameters as well. 还有其他参数。

You can't use commas within a parameter; 你不能参数中使用逗号; commas separate parameters. 逗号分开的参数。 Calling fun(x=a,b,c) would call fun() with three parameters, the first one named and the second two unnamed. 调用fun(x=a,b,c)将使用三个参数调用fun() ,第一个命名,第二个未命名。 If you just want to ignore the name x= , you can turn unquoted names to strings with 如果您只想忽略名称x= ,则可以将不带引号的名称转换为字符串

fun <- function(...){
  x <- sapply(substitute(...()), deparse)
  fun_(x)
}

fun_ <- function(x) {
    print(x)
}

fun(a,b,c)
# [1] "a" "b" "c" 

fun_(c("a","b","c"))
# [1] "a" "b" "c"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM