How to extend the ellipsis argument (...) without evaluating it in R

I need a function that takes an arbitrary number of arguments and stores them in a variable as an expression without evaluating them. I managed to do this with match.call , but it seems a bit "kludgy".

 foo <- function(...) { expr <- match.call() expr[[1]] <- expression expr <- eval(expr) # do some stuff with expr return(expr) } > bla Error: object 'bla' not found > foo(x=bla, y=2) expression(x = bla, y = 2) 

Explanation

To clarify, I ask how to write a function that behaves like expression() . I cannot use expression() directly for reasons that explain too long.

+4
source share
3 answers

The most idiomatic way:

 f <- function(x, y, ...) { match.call(expand.dots = FALSE)$`...` } 
+12
source

Use . from plyr as a prototype

 foo <- function (...) { as.expression(as.list(match.call()[-1])) } 
+4
source

The end result is a bit vague (could you clarify a little?). However, this may be useful:

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

Example:

 foo2(x=bla, y=2) # expression(x = bla, y = 2) 
+2
source

Source: https://habr.com/ru/post/1445658/


All Articles