Re: [R] Testing for existence inside a function

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Tue, 15 May 2007 15:51:01 -0400

On 5/15/2007 3:06 PM, Alberto Monteiro wrote:
> Talbot Katz wrote:

>> 
>> I'm having trouble testing for existence of an object inside a function.
>> 

> No, you are having trouble testing for existence of an object
> _before_ the function is called :-)
>
>> Suppose I have a function:
>> 
>> f<-function(x){
>> ...
>> }
>> 
>> and I call it with argument y:
>> 
>> f(y)
>> 
>> I'd like to check inside the function whether argument y exists.
>>

> This can't be done, because the error happens before f is called.
>
> Try this:
>
> f <- function(x) x + 1
> f(y.does.not.exist)
> y.does.not.exist
>
> The error message is (almost) the same, and it happens when
> parsing the line. There's no way to change f to change this.

That description is true in some languages, but not in R. R doesn't check that args to functions are valid until it needs to use them. For example:

 > f <- function(y) 1 # doesn't care if y exists  > f(y.does.not.exist)
[1] 1



R-help_at_stat.math.ethz.ch mailing list
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. Received on Tue 15 May 2007 - 19:58:24 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 May 2007 - 20:33:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.