[Rd] Re: [R] More user-friendly error message needed.

Warnes, Gregory R gregory_r_warnes at groton.pfizer.com
Tue Apr 20 15:12:57 CEST 2004



> -----Original Message-----
> From: r-devel-bounces at stat.math.ethz.ch
> [mailto:r-devel-bounces at stat.math.ethz.ch]On Behalf Of Douglas Bates
> Sent: Wednesday, April 07, 2004 5:13 PM
> To: Warnes, Gregory R
> Cc: Shin; r-devel at stat.math.ethz.ch; 'Duncan Murdoch'
> Subject: Re: [Rd] Re: [R] More user-friendly error message needed.
> 
> 
> "Warnes, Gregory R" <gregory_r_warnes at groton.pfizer.com> writes:
> 
> > 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.
> 
> Because of partial matching, list$name can return a result when 
> your has.element function returns FALSE.

Ash, well simple enough to fix:

	has.element <- function(list,name) !is.na(pmatch(name,names(list))

> 
> > lst = list(foo = 1:3, bar = LETTERS[1:3])
> > lst$f
> [1] 1 2 3
> > has.element <- function(list, name) name %in% names(list)
> > has.element(lst, "f")
> [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.

While I do think that it is necessary to preseve the present semantics, it
would still be helpful to (at least optionally) have a warning issued when
NULL is returned because there is no matching name.   This really would help
in debugging code.

-Greg


> 
> ______________________________________________
> R-devel at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-devel
> 


LEGAL NOTICE\ Unless expressly stated otherwise, this messag...{{dropped}}



More information about the R-devel mailing list