[Rd] NextMethod ignoring changed argument

Gabor Grothendieck ggrothendieck at myway.com
Wed Oct 27 15:21:39 CEST 2004


Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote:
:  
: Gabor Grothendieck wrote:
: 
: > 
: > Is this a bug? We define the [ operator for class test.
: > All it does is force drop to be FALSE and then calls
: > the NextMethod. If we specify drop= in the call then 
: > it works as expected (### 2 and ### 3) but if we do not
: > specify drop (### 1) then it acts as if drop=TRUE
: > even though we have set it to FALSE within [.test .
: > 
: > 
: >>"[.test" <- function(x, i, j, drop = TRUE) { 
: > 
: > + drop <- FALSE
: > + NextMethod("[") 
: > + }
: > 
: >>x <- structure(matrix(1:12, 4, 3), class = "test")
: > 
: > 
: >>x[1,] ### 1 - why does it ignore drop=FALSE in [.test ????
: > 
: > [1] 1 5 9
: > 
: > 
: >>x[1,,drop = TRUE] ### 2 - ok
: > 
: > [,1] [,2] [,3]
: > [1,] 1 5 9
: > 
: >>x[1,,drop = FALSE] ### 3 - ok
: > 
: > [,1] [,2] [,3]
: > [1,] 1 5 9
: > 
: >>R.version.string # windows XP
: > 
: > [1] "R version 2.0.0, 2004-10-04"
: > 
: 
: 
: Well, the *arguments* of the encolsing function are taken, but not the 
: objects defined therein. What you can do is:
: 
: "[.test" <- function(x, i, j, drop = TRUE) {
: NextMethod("[", drop = FALSE)
: }
: 
: Uwe Ligges

Thanks.  That worked.  Is that a workaround or is it a bug?
Note that if I set both i and drop within the [.test method then
the modified i gets passed but the modified drop does not:

R> "[.test" <- function(x, i, j, drop = TRUE) {
+      i <- 2; drop <- FALSE
+      NextMethod("[")
+ }
R> x <- structure(matrix(1:12, 4, 3), class = "test")
R> x[1,]
[1]  2  6 10



More information about the R-devel mailing list