[R] programming questions

Erik Iverson eriki at ccbr.umn.edu
Wed Nov 3 20:40:50 CET 2010

> alas, should R not come with an is.defined() function?  


a variable may
> never have been created, and this is different from a variable
> existing but holding a NULL.  this can be the case in the global
> environment or in a data frame.
>   > is.null(never.before.seen)
>   Error: objected 'never.before.seen' not found
>   > is.defined(never.before.seen)  ## I need this, because I do not
> want an error:
>   [1] FALSE

exists("never.before.seen") #notice the quotes

> your acs function doesn't really do what I want, either, because {
> d=data.frame( x=1:4); exists(acs(d$x)) } tells me FALSE .  I really
> need
>   > d <- data.frame( x=1:5, y=1:5 )
>   > is.defined(d$x)
>   TRUE

with(d, exists("x"))

>   > is.defined(d$z)

with(d, exists("z"))

>   > is.defined(never.before.seen)


>   > is.defined(never.before.seen$anything)  ## if a list does not
> exist, anything in it does not exist either

This one I'm a bit confused about.  If you're
programming a function, then the user either:

1) passes in an object, which is bound to a
local variable, and therefore exists. You can
do checks on that object to see that it conforms
to any constraints you have set.

2) does not pass in the object, in which case
you can test for that with ?missing.

Is writing your own functions for others to
use what you're doing?


More information about the R-help mailing list