[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-devel
Subject:    Re: [Rd] Ellipsis to Two Functions When One Has Nested Functions
From:       Brian Lee Yung Rowe <rowe () muxspace ! com>
Date:       2014-05-28 20:03:22
Message-ID: 9C79AEDF-D71B-411A-9F19-F2E87FC606AC () muxspace ! com
[Download RAW message or body]

Another option is to use a closure, which isolates the arguments explicitly.

aFunction <- function(data, alpha, ...)
{
   transform(alpha, ...)
   rowMeans(data) > alpha
}

f <- function(data, selection, ...)
{
   selected <- selection(data) # Remove ellipsis from here
   plot(data[selected, ], ...)
}

f(aDataset, function(data) aFunction(data, alpha = 10, transform = sqrt), pch = 19)

•••••
Brian Lee Yung Rowe
Founder, Zato Novo
Professor, M.S. Data Analytics, CUNY




On May 28, 2014, at 7:34 AM, Duncan Murdoch <murdoch.duncan@gmail.com> wrote:


> On 28/05/2014, 2:00 AM, Dario Strbenac wrote:
> > Hello
> > 
> > If I have a function
> > 
> > aFunction <- function(data, alpha, ...)
> > {
> > transform(alpha, ...)
> > rowMeans(data) > alpha
> > }
> > 
> > f <- function(data, selection, ...)
> > {
> > selected <- selection(data, ...)
> > plot(data[selected, ], ...)
> > }
> > 
> > f(aDataset, aFunction, alpha = 10, pch = 19, transform = sqrt)
> > 
> > and selection calls another function, which has ... and that function calls other \
> > functions, is there an easier way than getting the formals of all the functions \
> > called internally by selection, and all of the formals of functions called by \
> > plot ? Is there any option to change the error into a warning message ?
> 
> You can use try() to catch an error, but you can't ignore it.  The code that \
> triggered the error will still exit at that point. 
> There are at least two common ways of handling the situation where you want to pass \
> optional args to two different functions.  One is to use 
> args <- list(...)
> 
> to get all the arguments in a list, and then manually split up the list, and use \
> do.call() to pass some of them on.  Generally you would do this only if you knew in \
> advance how to split the args, you wouldn't try to call formals() to work it out at \
> run-time. 
> Another way is to have an argument like "control" in optim(), and ask the user to \
> pass one set of options via that, and the other via ... . You'd use do.call() again \
> to construct that call. 
> Duncan Murdoch
> 
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


	[[alternative HTML version deleted]]



______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic