Ellipsis to Two Functions When One Has Nested Functions

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Ellipsis to Two Functions When One Has Nested Functions

Dario Strbenac-2
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 ?

--------------------------------------
Dario Strbenac
PhD Student
University of Sydney
Camperdown NSW 2050
Australia

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Ellipsis to Two Functions When One Has Nested Functions

Duncan Murdoch-2
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Ellipsis to Two Functions When One Has Nested Functions

Brian Lee Yung Rowe
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Ellipsis to Two Functions When One Has Nested Functions

Brian Rowe
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel