简体   繁体   English

对多个字符串使用deparse(substitute)

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

相关问题 使用 sym() 和 deparse(substitute()) 的 Function 未按预期工作 - Function using sym() and deparse(substitute()) not working as expected 在lapply?deparse(substitute(x))? - deparse(substitute(x)) in lapply? deparse(substitute()) 与 lapply - deparse(substitute()) with lapply 使用`deparse(substitute))(或替代)时如何处理空格? - How can I handle whitespace when using `deparse(substitute))` (or an alternative)? 在函数调用中使用deparse(substitute())后覆盖全局环境中的对象 - Overwriting an object in the global environment after using deparse(substitute()) in a function call 使用data.table作为参数在函数内使用deparse(substitute()) - deparse(substitute()) within function using data.table as argument 使用deparse(substitute())将多个小对象导出到XLSX — sheetName问题 - Exporting multiple tibbles to XLSX — sheetName problem when using deparse(substitute()) 在R中向量化&#39;deparse(substitute(d))&#39;吗? - Vectorizing 'deparse(substitute(d))' in R? R:向量化解析(替代(x)) - R: Vectorize deparse(substitute(x)) 如何使用assign(deparse(substitute(df)))将相同的函数应用于多个数据帧以覆盖输入变量? [R] - How to apply the same functions to multiple data frames to overwrite input variables using assign(deparse(substitute(df)))? [R]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM