[Rd] asking for advice on how to catch nonexistent list elements

Tamas K Papp tpapp at Princeton.EDU
Wed Feb 28 03:15:27 CET 2007


Hi,

I am sorry if this is documented or in the archives somewhere, I tried
searching and could not find anything.

When using the $ operator to get a nonexistent element (or if you
prefer, all elements are defined as NULL by default) of a list, it
returns NULL, eg

> a <- list(maximum=12)
> a$maximum
[1] 12
> a$maxium  # typo
NULL

I know that this is the documented behavior of $, so this is
definitely not a bug.  But it leads to bugs that are quite elusive: an
error will only be generated when some function eventually chokes on
NULL.

I would like to know how people cope with that, and if there is a way
to generate an error message (at least when debugging code) for the
typo above.

Thanks,

Tamas



More information about the R-devel mailing list