R-beta: 'all.names' function -- failing as.list( _function_ )

Douglas Bates bates at stat.wisc.edu
Mon Apr 28 20:29:02 CEST 1997


Martin Maechler <maechler at stat.math.ethz.ch> writes:

> your 'all.names' function
> 	[wow, I didn't even know it in S..]
> seems to have been written with S in your mind;

Actually it was written for a specific purpose where we were examining
some formulas.  I was not thinking of applying it to functions when I
wrote it.

It is genuine R rather than S.  I compare the mode to "symbol" rather
than to "name".

> you are exactly demonstrating some of the 'fine' differences between  R & S
> 
> >1> all.names <- function (x) 
> >2> {
> >3>   if (mode(x) == "symbol")  return(as.character(x))
> >4>   if (length(x) == 0)       return(NULL)
> >5>   if (is.recursive(x))      return(unlist(lapply(as.list(x), all.names)))
> >6>   character(0)
> >7> }   
> 
> 1) length(x) is not always defined in R; e.g. it is NOT for functions.
>    --> Delete line 4  

It does inhibit portability of code that length(object) sometimes
produces NA in R.  At present the length of a symbol also is NA.  That
is why the test for mode of "symbol" occurs before the test for zero
length.  Ross has indicated that he will change that result in a future
release.

I agree that line 4 is superfluous in the current version.  In earlier
versions I was doing the recursion a bit differently so I needed a
check for zero length in there.

> 2) functions  are NOT lists and cannot be coerced to,
>    which makes line 5 fail for function objects.

Right O.  I don't know how to handle that either.

Some of the "eternal truths" about the S language are:
 - every object has a mode obtainable by mode(object)
 - every object has a length obtainable by length(object)
 - every object can be coerced to a list of the same length 
One can imagine that code that messes around with functions and other
expressions in R will break fairly quickly when these conditions do
not hold.  I don't know how much work would be involved in patching
over these differences between R and S but I suspect it would not be a
trivial undertaking.

There is an important omission in the current version of all.names.
Line 5 in your numbering should read
 if (is.recursive(x))  return(unique(unlist(lapply(as.list(x), all.names))))
Otherwise a large expression will produce a lot of duplicates.
-- 
Douglas Bates                            bates at stat.wisc.edu
Statistics Department                    608/262-2598
University of Wisconsin - Madison        http://www.stat.wisc.edu/~bates/
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



More information about the R-help mailing list