【问题标题】:Extracting symbols from the ellipsis argument of a function in a referentially transparent manner以引用透明的方式从函数的省略号参数中提取符号
【发布时间】:2015-03-23 01:10:08
【问题描述】:

又发生了。一个我刚要按下按钮发布答案的问题被删除了:

我正在寻找一种方法来从函数的省略号参数中提取绑定到符号以及符号的对象的值。也就是说,我试图以引用透明的方式从省略号中提取符号。我尝试过使用替代和lazy_dots 并没有成功。

 # function I'm trying to create
 get_symbols <- function(...) {
     # insert code here
     # should return a character vector of symbols
 }

# example function calls
some_function <- function(...){
    get_symbols(...)
}

z <- quote(T)
some_function(quote(x), ~y, z)
# should return vector of symbols c(x, ~y, T)

some_function(quote(x), ~y, quote(T))
# should return vector of symbols c(x, ~y, T)

【问题讨论】:

    标签: r


    【解决方案1】:

    看看substitutedeparse 的这些用法是否有帮助。 deparse 函数从(有效的)语言表达式返回文本/字符向量。 :

     some_function1 <- function(...){
             print(sapply( substitute(list(...)), deparse)[-1]) # -1 drops "list"
         }
    
         z <- quote(T)
         some_function1(quote(x), ~y, z)
    #[1] "quote(x)" "~y"       "z"       
         some_function1(quote(x), ~y, quote(T))
    #[1] "quote(x)" "~y"       "quote(T)"
    

    这个版本评估它的参数,这可能是期望的第二部分,即值:

     some_function2 <- function(...){
             print(sapply(list(...), deparse))
         }
    
         z <- quote(T)
         some_function2(quote(x), ~y, z)
    #[1] "x"  "~y" "T" 
         some_function2(quote(x), ~y, quote(T))
    #[1] "x"  "~y" "T" 
    

    【讨论】:

      猜你喜欢
      • 2017-12-22
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多