[R] Testing for arguments in a function

Duncan Murdoch murdoch.duncan at gmail.com
Tue Sep 27 07:17:30 CEST 2011


On 11-09-26 8:49 PM, David Winsemius wrote:
>
> On Sep 26, 2011, at 8:04 PM, Duncan Murdoch wrote:
>
>> On 11-09-26 5:15 PM, David Winsemius wrote:
>>>
>>> On Sep 26, 2011, at 4:56 PM, Duncan Murdoch wrote:
>>>
>>>> On 26/09/2011 3:39 PM, Gene Leynes wrote:
>>>>> I don't understand how this function can subset by i when i is
>>>>> missing....
>>>>>
>>>>> ## My function:
>>>>> myfun = function(vec, i){
>>>>>      ret = vec[i]
>>>>>      ret
>>>>> }
>>>>>
>>>>> ## My data:
>>>>> i = 10
>>>>> vec = 1:100
>>>>>
>>>>> ## Expected input and behavior:
>>>>> myfun(vec, i)
>>>>>
>>>>> ## Missing an argument, but error is not caught!
>>>>> ## How is subsetting even possible here???
>>>>> myfun(vec)
>>>>
>>>> Subsetting allows missing arguments.  What you have is equivalent to
>>>> evaluating
>>>>
>>>> vec[]
>>>>
>>>> which is legal.
>>>
>>> But I don't think "vec[]" is what he is seeing. At least it's not
>>> what
>>> I see. I see 10 coming back. I assumed it was simply because "i" was
>>> not found inside the function so its calling environment was examined
>>> so that vec[10] was returned.
>>>
>>
>> In which R version?
>
> Sorry for the confusion. Even before my wine with dinner I was under
> the impression that I had entered
>
> myfun(vec) # and gotten 10.
>
> When I  look back, I see  I was mistaken. So now I standing alongside
> Leynes. I thought R would find "i" and he thought it wouldn't. You are
> saying R "doesn't care" andthat should continue on it merry way. Why
> doesn't R look for "i"?
>

It does, and finds it bound to the mysterious "missing" marker.  So then 
as Gabor said, its missingness is passed on until some function actually 
uses it.  But that function is the [] subsetting function, which doesn't 
care that its argument is missing.

Duncan Murdoch



More information about the R-help mailing list