简体   繁体   中英

Using deparse(substitute) for several strings

The combination of deparse(substitute) seems useful. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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