[Rd] Scoping bug in ftable() (PR#6541)

dmurdoch at pair.com dmurdoch at pair.com
Wed Feb 4 19:32:30 MET 2004


This bug shows up in ftable() in both r-patched and r-devel:

> x <- c(1,2)
> y <- c(1,2)
> z <- c(1,1)
> ftable(z,y)
  y 1 2
z      
1   1 1
> ftable(z,x)
  x 1
z    
1   2

Since x and y are identical, the two ftable results should be the
same, but they are not.  

I've only been able to see this when the column variable is named "x",
so it looks like a scoping problem in ftable.default.  I think the
problem is in this line:

x <- do.call("table", c(as.list(substitute(list(...)))[-1],
list(exclude = exclude)))

I think this call is finding the local variable "x" (which has been
used before this line) instead of the argument "x" and thus produces
an incorrect result.

How should this be fixed?  What we want is to convert "..." into an
evaluated list that includes the deparsed arguments as names.  Just
plain list(...) loses the names.

I think this works:

args <- list(...)
names(args) <-
as.character(unlist(as.list(substitute(list(...)))))[-1]
x <- do.call("table", args)

but isn't there an easier way?

Duncan Murdoch



More information about the R-devel mailing list