[英]Using deparse(substitute) for several strings
The combination of deparse(substitute)
seems useful. deparse(substitute)
的组合似乎很有用。 However, how can I use this for several strings. 但是,如何将其用于几个字符串。 Working example:
工作示例:
print_name<-function(x){
x<-deparse(substitute(x))
print(x)
}
How can I make this work for something like this? 我怎样才能使这种工作呢?
print_name<-function(x,...){
x<-deparse(substitute(x))
y<-deparse(substitute(...))
print(x)
print(y)
}
print_name(Peter,John,Alice)
The above almost works except it stops at(on?) John. 除了停止在(John)上以外,以上几乎可以工作。 How can I make it work for all names.
如何使它适用于所有名称。 That is print Peter,John,Alice.
那是彼得,约翰,爱丽丝的版画。 Thanks!
谢谢!
You can do this 你可以这样做
print_name<-function(x,...){
x<-deparse(substitute(x))
y<-sapply(substitute(...()), deparse)
print(x)
print(y)
}
print_name(Peter,John,Alice)
# [1] "Peter"
# [1] "John" "Alice"
If we do substitute(...())
we will get a list-like object of all the symbols, and we just sapply over them to turn each of them into a character value 如果我们
substitute(...())
我们将得到所有符号的类似列表的对象,然后将它们应用于所有符号,即可将每个符号转换为字符值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.