[Rd] Inconsistency with component names

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Mar 4 17:39:58 MET 2004


Duncan Murdoch <dmurdoch at pair.com> writes:

> Working in r-devel, we're not allowed to use a list component name of
> "":
> 
> > list(a = 1, "" = 2)
> Error: attempt to use zero-length variable name
> 
> However, that's what gets reported to us if we create a list with no
> name:
> 
> > x <- list(a = 1, 2)
> > names(x)
> [1] "a" "" 
> 
> The names<- function apparently doesn't check for the error:
> 
> > x <- list(1, 2)
> > names(x) <- c("a", "")
> > names(x)
> [1] "a" "" 
> 
> and it allows all names to be blank:
> 
> > names(x) <- c("", "")
> > names(x)
> [1] "" ""
> 
> However, if we do it with a piece of language, things are different:
> 
> > x <- quote(list(1,2))
> > x
> list(1, 2)
> > mode(x)
> [1] "call"
> > names(x)
> NULL
> > names(x) <- c("", "a", "")
> > x
> list(a = 1, 2)
> > names(x) <- c("", "", "")
> > x
> list(1, 2)
> > names(x)
> NULL
> 
> I think the behaviour should be consistent between lists and call
> objects; if the behaviour for calls followed the behaviour for lists,
> it would let me save a line (if (is.null(names(x)) .... ) in the
> margins function.
> 
> Even though they print the same way, names of c("", "", "") are
> different from no names at all.

This goes deeper than you might be thinking. The problem is that in
certain positions the R syntax (ab)uses double quotes for things that
aren't character objects but language symbols. With the new backtick
quotes, we can write list(`foo bar` = 1) and we really ought to do
that consistently (except that there are massive back- and S
compatibility issues). However, you can't have a zero-length symbol
(partly because we're using them to represent missing arguments in
some cases) as you'll see by typing `` or as.symbol("").

Same thing with "" <- 1234 and ""(x). However, x$"" is allowed since
the parser there actually does the reverse process and converts names
to character strings:


> class(x) <- "fee"
> "$.fee" <- function(x, i, ...) match.call()
> x$""
"$.fee"(x = x, i = "")
> x$a
"$.fee"(x = x, i = "a")

(notice that the a in x$a is converted to "a")

We might be able to allow zero-length variable names, but we'd  have to
be extremely careful. We do use the missing object in a few places,
but I don't know if the implementation as a zero length name matters.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907



More information about the R-devel mailing list