[Rd] Possible bug in `class<-` when a class-specific '[[.' method is defined

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Jul 15 14:57:12 CEST 2019


Hello,

Clean R 3.6.1 session on Ubuntu 19.04, RStudio 1.1.453. sessionInfo() at 
the end.

I can reproduce this.

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] 9


But there's more. I tried to print the values of x in the method and got 
really strange results

counttt <- 0

`[[.MYCLASS` = function(x, ...) {
   counttt <<- counttt + 1
   print(x)
   # browser()
   x = NextMethod()
   return(x)
}

df <- as.data.frame(matrix(1:20, nrow=5))
class(df) <- c("MYCLASS","data.frame")
counttt
#[1] 151


If I change print to print.data.frame it goes up to

counttt
#[1] 176

With print.default back to 9. What is the print method called in the 
second example?


sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 19.04

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0

locale:
  [1] LC_CTYPE=pt_PT.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=pt_PT.UTF-8        LC_COLLATE=pt_PT.UTF-8
  [5] LC_MONETARY=pt_PT.UTF-8    LC_MESSAGES=pt_PT.UTF-8
  [7] LC_PAPER=pt_PT.UTF-8       LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=pt_PT.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods
[7] base

loaded via a namespace (and not attached):
   [1] sos_2.0-0           nlme_3.1-140        matrixStats_0.54.0
   [4] fs_1.2.7            xts_0.11-2          usethis_1.5.0
   [7] lubridate_1.7.4     devtools_2.0.2      RColorBrewer_1.1-2
  [10] rprojroot_1.3-2     rbenchmark_1.0.0    tools_3.6.1
  [13] backports_1.1.4     R6_2.4.0            rpart_4.1-15
  [16] Hmisc_4.2-0         lazyeval_0.2.2      colorspace_1.4-1
  [19] nnet_7.3-12         npsurv_0.4-0        withr_2.1.2
  [22] tidyselect_0.2.5    gridExtra_2.3       prettyunits_1.0.2
  [25] processx_3.3.0      curl_3.3            compiler_3.6.1
  [28] cli_1.1.0           htmlTable_1.13.1    randomNames_1.4-0.0
  [31] dvmisc_1.1.3        desc_1.2.0          tseries_0.10-46
  [34] scales_1.0.0        checkmate_1.9.1     lmtest_0.9-36
  [37] fracdiff_1.4-2      mvtnorm_1.0-10      quadprog_1.5-6
  [40] callr_3.2.0         stringr_1.4.0       digest_0.6.18
  [43] foreign_0.8-71      rio_0.5.16          base64enc_0.1-3
  [46] stocks_1.1.4        pkgconfig_2.0.2     htmltools_0.3.6
  [49] sessioninfo_1.1.1   readxl_1.3.1        htmlwidgets_1.3
  [52] rlang_0.3.4         TTR_0.23-4          rstudioapi_0.10
  [55] quantmod_0.4-14     MLmetrics_1.1.1     zoo_1.8-5
  [58] zip_2.0.1           acepack_1.4.1       dplyr_0.8.0.1
  [61] car_3.0-2           magrittr_1.5        Formula_1.2-3
  [64] Matrix_1.2-17       Rcpp_1.0.1          munsell_0.5.0
  [67] abind_1.4-5         stringi_1.4.3       forecast_8.6
  [70] yaml_2.2.0          carData_3.0-2       MASS_7.3-51.3
  [73] pkgbuild_1.0.3      plyr_1.8.4          grid_3.6.1
  [76] parallel_3.6.1      forcats_0.4.0       crayon_1.3.4
  [79] lattice_0.20-38     haven_2.1.0         splines_3.6.1
  [82] hms_0.4.2           knitr_1.22          ps_1.3.0
  [85] pillar_1.4.0        pkgload_1.0.2       urca_1.3-0
  [88] glue_1.3.1          lsei_1.2-0          babynames_1.0.0
  [91] latticeExtra_0.6-28 data.table_1.12.2   remotes_2.0.4
  [94] cellranger_1.1.0    testthat_2.1.0      gtable_0.3.0
  [97] purrr_0.3.2         assertthat_0.2.1    ggplot2_3.1.1
[100] openxlsx_4.1.0      xfun_0.6            survey_3.35-1
[103] survival_2.44-1.1   timeDate_3043.102   tibble_2.1.1
[106] memoise_1.1.0       cluster_2.0.8       toOrdinal_1.1-0.0
[109] fitdistrplus_1.0-14 brew_1.0-6



Hope this helps,

Rui Barradas


Às 13:16 de 15/07/19, Duncan Murdoch escreveu:
> 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
> 
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list