Skip to content
Back to formatted view

Raw Message

Message-ID: <747cf507-e25b-d05f-366d-7f1ea2ddcbcd@gmail.com>
Date: 2019-07-15T12:16:57Z
From: Duncan Murdoch
Subject: Possible bug in `class<-` when a class-specific '[[.' method is defined
In-Reply-To: <1be44ffeed8e46619c57a9f8fae974b6@epfl.ch>

On 07/07/2019 11:49 a.m., Ghiggi Gionata wrote:
> Hi all !
> 
> I noticed a strange behaviour of the function `class<-` when a class-specific '[[.' method is defined.
> 
> Here below a reproducible example :
> 
> 
> #-------------------------------------------------------------------.
> 
> counttt <- 0
> 
> `[[.MYCLASS` = function(x, ...) {
>    counttt <<- counttt + 1
>    # browser()
>    x = NextMethod()
>    return(x)
> }
> 
> df <- as.data.frame(matrix(1:20, nrow=5))
> class(df) <- c("MYCLASS","data.frame")
> counttt
> 
> # The same occurs when using structure(, class=) or attr(,"class")<-
> df <- as.data.frame(matrix(1:20, nrow=5))
> df <- structure(df, class=c("MYCLASS","data.frame"))
> attr(df, "class") <- c("MYCLASS","data.frame")
> 
> #-------------------------------------------------------------------.
> 
> Why in this example `class<-` is calling  `[[.MYCLASS` 9 times ?
> 
> Is there a way to avoid `class<-` to call `[[.MYCLASS` ?
> 
> 
> Thank you in advance for your help and suggestions.

This is what I see:


 > counttt <- 0
 >
 > `[[.MYCLASS` = function(x, ...) {
+   counttt <<- counttt + 1
+   # browser()
+   x = NextMethod()
+   return(x)
+ }
 >
 > df <- as.data.frame(matrix(1:20, nrow=5))
 > class(df) <- c("MYCLASS","data.frame")
 > counttt
[1] 0

So there's something else going on in your system.  Maybe post 
sessionInfo()?

Duncan Murdoch