Dear R developers,

while experimenting with repeated measures ANOVA, I found out that it is possible to construct a model specification that is syntactically valid, but causes aov() to abort with an error. A minimal reproducer and its output are attached to this mail. I was able to reproduce this problem with the latest SVN revision.

The root cause is similar to that of bug 15377: aov() calls deparse() on the model specification. If the resulting string is too long, e.g. due to long column names, deparse() performs line breaking and returns a vector of strings, which aov() does not handle correctly.

The attached patch fixes this problem by making aov() call deparse1(). It also corrects an error in the documentation of deparse1().

Please CC me on replies as I am not subscribed to the mailing list.

With best regards,
  Jan Hauffa
Index: src/library/base/man/deparse.Rd
===================================================================
--- src/library/base/man/deparse.Rd     (revision 78196)
+++ src/library/base/man/deparse.Rd     (working copy)
@@ -28,7 +28,7 @@
     See \code{\link{.deparseOpts}}.}
   \item{nlines}{integer: the maximum number of lines to produce.
     Negative values indicate no limit.}
-  \item{collapse}{a string, passed to \code{\link{parse}()}.}
+  \item{collapse}{a string, passed to \code{\link{paste}()}.}
   \item{\dots}{further arguments passed to \code{deparse()}.}
 }
 \details{
Index: src/library/stats/R/aov.R
===================================================================
--- src/library/stats/R/aov.R   (revision 78196)
+++ src/library/stats/R/aov.R   (working copy)
@@ -48,7 +48,7 @@
     } else {
        if(pmatch("weights", names(Call), 0L))
             stop("weights are not supported in a multistratum aov() fit")
-        deparseb <- function(expr) deparse(expr, width.cutoff = 500L, backtick 
= TRUE)
+        deparseb <- function(expr) deparse1(expr, width.cutoff = 500L, 
backtick = TRUE)
         ##  Helmert contrasts can be helpful: do we want to force them?
         ##  this version does for the Error model.
         opcons <- options("contrasts")
> source('testcase.R')
Error in str2expression(x) : <text>:2:5: unexpected symbol
1: ame10, someReallyLongVariableName11, someReallyLongVariableName12, 
someReallyLongVariableName13, someReallyLongVariableName14, 
someReallyLongVariableName15, someReallyLongVariableName16, someR
2:     someReallyLongVariableName18
       ^
In addition: Warning message:
Using formula(x) is deprecated when x is a character vector of length > 1.
  Consider formula(paste(x, collapse = " ")) instead. 
> traceback()
9: str2expression(x)
8: formula.character(object, env = baseenv())
7: formula(object, env = baseenv())
6: as.formula(paste(deparseb(formula[[2L]]), "~", deparseb(errorterm[[2L]]), 
       if (!intercept) "- 1"), env = environment(formula))
5: aov(model, data) at testcase.R#9
4: eval(ei, envir)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("testcase.R")
data <- data.frame("index" = 1:50)
names <- c()
for (i in 1:20) {
        names <- c(names, paste("someReallyLongVariableName", i, sep = ""))
        data[[names[i]]] <- rnorm(50)
}
names <- factor(names)
model <- as.formula(paste("cbind(", paste(names, collapse = ","), ") ~ 1 + 
Error(1)", sep = ""))
result <- aov(model, data)
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to