Not sure if we can do much about this now or in the future, but I'm recording it here in case we update something in the future?
The following example illustrates a scoping bug/infelicity that, ideally, we'd be able to handle in vegan
library("vegan")
data(dune, dune.env)
foo <- function(x, env) {
m <- rda(x ~ Manure + A1, data = env)
anova(m, by = "margin")
}
out <- lapply(dune, foo, env = dune.env)
which fails with:
> out <- lapply(dune, foo, env = dune.env)
Error in eval(expr, envir, enclos) : object 'x' not found
the traceback() is:
> traceback()
18: eval(expr, envir, enclos)
17: eval(expr, p)
16: eval.parent(specdata, n = envdepth)
15: ordiParseFormula(formula, data, na.action = na.action, subset = substitute(subset))
14: rda.formula(formula = x ~ A1, data = env)
13: rda(formula = x ~ A1, data = env)
12: eval(expr, envir, enclos)
11: eval(call, parent.frame())
10: update.default(object, paste(".~.-", nm))
9: update(object, paste(".~.-", nm))
8: permutest(update(object, paste(".~.-", nm)), permutations, ...)
7: FUN(c("Manure", "A1")[[1L]], ...)
6: lapply(trmlab, function(nm, ...) permutest(update(object, paste(".~.-",
nm)), permutations, ...), ...)
5: anova.ccabymargin(object, permutations = permutations, model = model,
parallel = parallel, scope = scope)
4: anova.cca(m, by = "margin")
3: anova(m, by = "margin") at #3
2: FUN(X[[1L]], ...)
1: lapply(dune, foo, env = dune.env)
The obvious workaround is
outs <- vector(mode = "list", length = NCOL(dune))
for (i in seq_len(NCOL(dune))) {
resp <- dune[, i, drop = FALSE]
m <- rda(resp ~ Manure + A1, data = dune.env)
outs[[i]] <- anova(m, by = "margin")
}
or, retaining lapply():
bar <- function(i, env) {
m <- rda(dune[,i] ~ Manure + A1, data = env)
anova(m, by = "margin")
}
out <- lapply(seq_len(NCOL(dune)), bar, env = dune.env)
Not sure if we can do much about this now or in the future, but I'm recording it here in case we update something in the future?
The following example illustrates a scoping bug/infelicity that, ideally, we'd be able to handle in vegan
which fails with:
the
traceback()is:The obvious workaround is
or, retaining
lapply():