[R] Variable Argument Function

Duncan Murdoch murdoch.duncan at gmail.com
Mon Feb 8 01:36:55 CET 2016


On 07/02/2016 7:14 PM, Ben Tupper wrote:
> Hi,
>
>> On Feb 7, 2016, at 6:24 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
>>
>> On 07/02/2016 6:12 PM, Robert Sherry wrote:
>>>
>>> I would like to write a function in R that would take a variable number
>>> of integers as parameters. I do not have a pressing reason to do this, I
>>> am just trying to learn R. I thought a good first step would be to print
>>> out the arguments. So I wrote the following function:
>>>
>>> f1 = function (...)
>>> {
>>>       list1 = as.list(...)
>>
>> This is wrong.  The ... object is weird; it's not something that can be coerced to a list.  However, you can pass it as list(...) and it will give you what you were expecting.
>>
>
> Do you mean that Bob should nest a function within f1?  Like this?

No need for that.  His original function would work if he had used 
list(...) instead of as.list(...).

Duncan Murdoch

>
> f1 = function (...){
>     f2 <- function(list1){
>        for( i in 1:length(list1) ) cat( "i is ", list1[[i]], "\n" )
>        return (0)
>      }
>      f2(list(...))
> }
>
> f1(2,4,10,12)
>
>> f1(2,4,10,12)
> i is  2
> i is  4
> i is  10
> i is  12
>
> Ben
>
>
>> The theory is that it will expand to multiple arguments to the list() function, which constructs a list containing them.  as.list() doesn't want a bunch of arguments, it will just ignore most of them.
>>
>> Duncan Murdoch
>>
>>>       for( i in 1:length(list1) )
>>>           cat( "i is ", list1[[i]], "\n" )
>>>       return (0)
>>> }
>>>
>>> I ran it as:
>>>       f1(2,4,10,12)
>>> and I get:
>>>       i is  2
>>>       [1] 0
>>> I was hoping for
>>>       i is  2
>>>       i is  4
>>>       i is  10
>>>       i is  12
>>>
>>> I am hoping somebody can tell me what I am doing wrong. Is using a list
>>> a bad idea?
>>>
>>> Thanks
>>> Bob
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>> ______________________________________________
>> 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.
>
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list