[Rd] obstacles to using vectors with attributes

Jim Regetz regetz at nceas.ucsb.edu
Wed Apr 16 01:37:39 CEST 2008

Hi all,

I am working on an application in which we'd like to use attributes to 
attach simple metadata to arbitrary R objects. It is essential that all 
functions that will work on the original object will also work on the 
object after metadata attributes have been added. However, this is not 
always the case for vectors: functions such as barplot, diffinv, and rle 
use is.vector, which (as documented and as discussed previously on this 
list) returns FALSE for a vector that has any attributes other than 
"names". For example,

 > x <- 1:3
 > attr(x, "date") <- date()
 > barplot(x)
Error in barplot.default(x) : 'height' must be a vector or a matrix

It strikes me that is.vector may be an overly strict test in these 
cases. Shouldn't barplot(x) behave the same regardless of whether vector 
x has any (non-special) attributes? In barplot.default, what if 
is.vector(x) were replaced with something along the lines of:
	is.atomic(x) && is.null(dim(x))

Admittedly, other functions currently using is.vector may require 
something slightly different. One such example is data.frame, which uses 
is.vector when applying recycling rules. It appears it is used at least 
in part to filter out AsIs objects, so a specific test of whether the 
object class is AsIs may be appropriate?

No doubt a slew of functions in user-contributed packages use is.vector 
in similar fashion, so even modifying core functions wouldn't fully 
solve our particular problem. On that note, I don't suppose there's hope 
of revisiting the definition of is.vector itself? Bear in mind, I do 
appreciate that there are good reasons for its current behavior, and 
compatibility constraints on changing it.


James Regetz, Ph.D.
Scientific Programmer/Analyst
National Center for Ecological Analysis & Synthesis
735 State St, Suite 300
Santa Barbara, CA 93101

More information about the R-devel mailing list