[英]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.