[R] Using names in function with ellipsis (non standard evaluation?)

Duncan Murdoch murdoch.duncan at gmail.com
Thu May 28 21:17:45 CEST 2015


On 28/05/2015 1:40 PM, Luca Cerone wrote:
> Hi everybody,
> 
> this is probably a silly question, but I can't find a way to recognize
> the names that are passed
> to variables in ellipsis.
> 
> For example, say I have a "core" function that receives some extra
> parameters through ...
> e.g.
> 
> f <- function(...) {
>    params <- c(...)
>    #dothehardworkhere using "names(params)"
> }
> 
> and then I want to create a function g where some of the parameters
> are set like:
> 
> g <- function(x,y) f(x,y)
> 
> I figure I probably have to use to substitute in f, but it is not
> clear to me how.
> 
> Definitely what I need to achieve is that when I call:
> 
> g(1,2) then in f params is the vector c(x=1,y=2);
> similarly I want to be able to call g(y=2, x=1)
> and have params = c(x=1,y=2) in f.
> 
> Can you please help me understanding how to do this?

Sorry, I misunderstood your question.  I didn't notice that g calls f.
You should write g to call f with names on the parameters, i.e.

g <- function(x,y) f(x=x, y=y)

then f will receive the parameters with names on them.  I'd still advise
against using c(...), but it will give you the output you want with that
input; the problem is if your users do something like
g(1:2, 3:4) (which would give c(x1=1, x2=2, y1=3, y2=4)).

Duncan Murdoch



More information about the R-help mailing list