Gavin Simpson
2009-Nov-03 20:05 UTC
[Rd] Standard non-standard evaluation problem with 2.10-0
Dear List I am getting an error when checking my analogue package with R2.10.0-patched. The error comes when running a function within which I use the standard non-standard evaluation method. I've distilled the error and functions involved out into the following simple example to illustrate the error: ## Dummy data to illustrate formula method d <- data.frame(A = runif(10), B = runif(10), C = runif(10)) ## simulate some missings d[sample(10,3), 1] <- NA foo <- function(formula, data = NULL, subset = NULL, na.action = na.pass, ...) { mf <- match.call() mf[[1]] <- as.name("model.frame") mt <- terms(formula, data = data, simplify = TRUE) mf[[2]] <- formula(mt, data = data) mf$na.action <- substitute(na.action) dots <- list(...) mf[[names(dots)]] <- NULL mf <- eval(mf,parent.frame()) mf } ## apply foo using formula foo(~ . - B, data = d, na.action = na.pass, method = "missing", na.value = 0) Error in mf[[names(dots)]] <- NULL : more elements supplied than there are to replace If I debug(foo) and do: Browse[2]> names(dots) [1] "method" "na.value" Browse[2]> names(mf) [1] "" "formula" "data" "na.action" "method" [6] "na.value" Browse[2]> mf[[names(dots)[1]]] [1] "missing" Browse[2]> mf[[names(dots)[2]]] [1] 0 But Browse[2]> mf[[names(dots)]] Error in mf[[names(dots)]] : subscript out of bounds Browse[2]> str(names(dots)) chr [1:2] "method" "na.value" I could have sworn I tested this during the beta test phase for 2.10.0 - if I did I didn't get any errors at that time - and this code works fine under R2.9.x branch. The package is now failing checks on CRAN and on my local install. Am I doing something patently stupid here? Has something changed in '[[' or 'names' that I'm now running foul of? I can probably work round this by setting the individual names of 'mf' to NULL in two calls, but I'd like to get to the bottom of the problem if at all possible. Session Info: R version 2.10.0 Patched (2009-11-01 r50276) i686-pc-linux-gnu locale: [1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8 [5] LC_MONETARY=C LC_MESSAGES=en_GB.UTF-8 [7] LC_PAPER=en_GB.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods [7] base other attached packages: [1] analogue_0.6-21 MASS_7.2-48 lattice_0.17-25 [4] vegan_1.15-3 loaded via a namespace (and not attached): [1] grid_2.10.0 tools_2.10.0 Thanks in advance, G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson
2009-Nov-03 20:36 UTC
[Rd] SOLVED: Re: Standard non-standard evaluation problem with 2.10-0
Dear list, Prof Ripley has replied with the solution - I /was/ doing something patently stupid. The offending line: mf[[names(dots)]] <- NULL should have been mf[names(dots)] <- NULL That the offending line worked in R 2.9.x was the result of bug, which has been fixed in the current version, and it was my mistake in using '[[' where I meant '['. All the best, Gavin On Tue, 2009-11-03 at 20:05 +0000, Gavin Simpson wrote:> Dear List > > I am getting an error when checking my analogue package with > R2.10.0-patched. The error comes when running a function within which I > use the standard non-standard evaluation method. I've distilled the > error and functions involved out into the following simple example to > illustrate the error: > > ## Dummy data to illustrate formula method > d <- data.frame(A = runif(10), B = runif(10), C = runif(10)) > ## simulate some missings > d[sample(10,3), 1] <- NA > > foo <- function(formula, data = NULL, > subset = NULL, > na.action = na.pass, ...) { > mf <- match.call() > mf[[1]] <- as.name("model.frame") > mt <- terms(formula, data = data, simplify = TRUE) > mf[[2]] <- formula(mt, data = data) > mf$na.action <- substitute(na.action) > dots <- list(...) > mf[[names(dots)]] <- NULL > mf <- eval(mf,parent.frame()) > mf > } > > ## apply foo using formula > foo(~ . - B, data = d, na.action = na.pass, > method = "missing", na.value = 0) > Error in mf[[names(dots)]] <- NULL : > more elements supplied than there are to replace > > If I debug(foo) and do: > > Browse[2]> names(dots) > [1] "method" "na.value" > Browse[2]> names(mf) > [1] "" "formula" "data" "na.action" "method" > [6] "na.value" > Browse[2]> mf[[names(dots)[1]]] > [1] "missing" > Browse[2]> mf[[names(dots)[2]]] > [1] 0 > > But > Browse[2]> mf[[names(dots)]] > Error in mf[[names(dots)]] : subscript out of bounds > Browse[2]> str(names(dots)) > chr [1:2] "method" "na.value" > > I could have sworn I tested this during the beta test phase for 2.10.0 - > if I did I didn't get any errors at that time - and this code works fine > under R2.9.x branch. The package is now failing checks on CRAN and on my > local install. > > Am I doing something patently stupid here? Has something changed in '[[' > or 'names' that I'm now running foul of? I can probably work round this > by setting the individual names of 'mf' to NULL in two calls, but I'd > like to get to the bottom of the problem if at all possible. > > Session Info: > R version 2.10.0 Patched (2009-11-01 r50276) > i686-pc-linux-gnu > > locale: > [1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C > [3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8 > [5] LC_MONETARY=C LC_MESSAGES=en_GB.UTF-8 > [7] LC_PAPER=en_GB.UTF-8 LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics grDevices utils datasets methods > [7] base > > other attached packages: > [1] analogue_0.6-21 MASS_7.2-48 lattice_0.17-25 > [4] vegan_1.15-3 > > loaded via a namespace (and not attached): > [1] grid_2.10.0 tools_2.10.0 > > Thanks in advance, > > G-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%