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

Bert Gunter bgunter.4567 at gmail.com
Thu May 28 22:49:57 CEST 2015


I also am not sure exactly what the OP wants and even less sure of what he
needs...

But a possible answer is that a canonical way to do this is just to pass
down the ... list in the definition and specifying a named list of
arguments in the call (as has already been mentioned).

e.g. consider:

> g <- function(f,...)f(...)
## so g can accept arbitrary functions with arbitrary arguments

> fru <- function(x,y=3)x+y

> g(fru,x=2) ## default y used
[1] 5

> g(fru,x=2,y=7) ## y argument given explicitly
[1] 9


Please pardon the noise if this is irrelevant.

Cheers,
Bert

On Thu, May 28, 2015 at 12:17 PM, Duncan Murdoch <murdoch.duncan at gmail.com>
wrote:

> 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
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list