How to expand an ellipsis (…) argument without evaluating it in R

后端 未结 3 1797
别跟我提以往
别跟我提以往 2020-12-06 02:50

I need a function that accepts an arbitrary number of arguments and stores them in a variable as an expression without evaluating them. I managed to do it with match.c

相关标签:
3条回答
  • 2020-12-06 03:13

    The most idiomatic way is:

    f <- function(x, y, ...) {
      match.call(expand.dots = FALSE)$`...`
    }
    
    0 讨论(0)
  • The ultimate intended outcome is slightly vague (could you clarify a bit?). However, this may be helpful:

    foo2 <- function(...) {
      expr <- as.list(substitute(list(...)))[-1L]
      class(expr) <- "expression"
      expr
    }
    

    example:

    foo2(x=bla, y=2)
    # expression(x = bla, y = 2)
    
    0 讨论(0)
  • 2020-12-06 03:31

    Using . from plyr as a prototype

    foo <-   function (...) 
      {
      as.expression(as.list(match.call()[-1]))
      }
    
    0 讨论(0)
提交回复
热议问题