How to get content ... like a call list?

If I want to see what expression is passed to the function, I can get it with substitute.

f <- function(x)
{
  substitute(x)  
}

f(sin(pi))
## sin(pi)

( freturns an object of the class call. substituteUsually combined with deparseto turn it into a character vector, but I'm not interested here.)

I want to repeat this with arguments in .... This attempt returns only the first argument:

g <- function(...)
{
  substitute(...)
}

g(sin(pi), cos(pi / 2))
## sin(pi)

This attempt causes an error:

h <- function(...)
{
  lapply(..., subsitute)
}

h(sin(pi), cos(pi / 2))
## Error in match.fun(FUN) :
##   'cos(pi/2)' is not a function, character or symbol

This attempt causes another error:

i <- function(...)
{
  lapply(list(...), substitute)
}

i(sin(pi), cos(pi / 2))

## Error in lapply(list(...), substitute) : 
##   '...' used in an incorrect context

How to get the expressions that I passed in ...?

+4
source share
2 answers

:

i <- function(...)
{
  l <- match.call()
  l <- as.list(l)
  l <- l[-1]
  l
}

i <- function(...)
{
  l <- match.call()
  l[[1]] <- as.name("expression")
  l
}
i(sin(pi), cos(pi/2))

, , match.call , . HTH

+3

:

substitute_multi <- function(...) {
   f <- function(e1, ...) {
      if (missing(e1)) return(NULL)
      else return(list(substitute(e1), substitute_multi(...)))
   }
   unlist(f(...))
}

, :

substitute_multi(x, g(y), 1+2+3)
## [[1]]
## x
## 
## [[2]]
## g(y)
## 
## [[3]]
## 1 + 2 + 3

as.expression , expression.

IMHO, , , , ... .:)

+1

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


All Articles