Skip to content

More user-friendly error message needed.

2 messages · Warnes, Gregory R, Douglas Bates

#
Perhaps one could create a utility function 

	has.element <- function(list, name) name %in% names(list)

and then have $ generate a warning (not an error!) when the named element
does not exist.

This would be helpful in debugging code.  Yesterday I spent quite some time
tracking down an error that turned out to be $ returning a NULL because the
data file I read in had a variable mis-named.   A warning message would have
made everything clear right away.

-G
LEGAL NOTICE\ Unless expressly stated otherwise, this messag...{{dropped}}
#
"Warnes, Gregory R" <gregory_r_warnes@groton.pfizer.com> writes:
Because of partial matching, list$name can return a result when 
your has.element function returns FALSE.
[1] 1 2 3
[1] FALSE

Before we go too far in speculating about what the $ operator should
or should not return when the name is not matched, let's all remember
that the semantics of the $ operator explicitly state that it should
return NULL and, as Duncan wrote, there are many, many places in the
base code for R that depend upon this behavior.  Trust me - you really
don't want to change this.