Search code examples
rfunction-call

Displaying the actual parameter list of the function during execution


I am trying to display the actual values of the parameters that were supplied when the function was called. `match.call' does something along the lines I want but it does not evaluate variables. For example

foo <- function(x) match.call()
foo(2)

prints

foo(x = 2)

and I am happy with that. However:

xxx <- 2
foo(xxx)

will print

foo(x = xxx)

instead of foo(x = 2) as I would like to have it.

I have tried various combinations of substitute, eval, and company, but was not succesful.


Solution

  • I don't know if this is the best way, but probably you can do it by:

    x<-"a"
    y<-mean
    z<-1
    foo <- function(x,y,z) {
      do.call("call", 
              c(list(as.character(match.call()[[1]])),
                lapply(as.list(match.call())[-1],eval)))
    }
    foo(x,y,z)