Search code examples
rdplyrr-s4

tbl_df with S4 Object Slots


Trying my first attempt as OO programming in R, I declared a S4 object, TestObject, that has two slots where one of the slots (test.df) has the permitted class of a data.frame:

library("dplyr")
library("methods")

# Declare S4 Object
setClass("TestObject",
  slots = list(test.mat = "matrix",
               test.df = "data.frame"))

To my surprise, when I try to assign an object of tbl_df “class”:

library("nycflights13")
class(flights)
## [1] "tbl_df"     "tbl"        "data.frame"

It throws an error:

test.object <- new("TestObject")
slot(test.object, "test.mat") <- matrix(rnorm(10), 5, 2)
slot(test.object, "test.df") <- flights
## Error in checkSlotAssignment(object, name, value): c("assignment of an object of class \"tbl_df\" is not valid for slot 'test.df' in an object of class \"TestObject\"; is(value, \"data.frame\") is not TRUE", "assignment of an object of class \"tbl\" is not valid for slot 'test.df' in an object of class \"TestObject\"; is(value, \"data.frame\") is not TRUE", "assignment of an object of class \"data.frame\" is not valid for slot 'test.df' in an object of class \"TestObject\"; is(value, \"data.frame\") is not TRUE")

I was under the impression that the tbl_df just wraps around a data.frame. I thought maybe I could declare the tbl_df as the permitted class for test.df:

setClass("TestObject",
  slots = list(test.mat = "matrix",
               test.df = "tbl_df"))
## Warning: undefined slot classes in definition of "TestObject":
## test.df(class "tbl_df")

But it doesn't appear to recognize it as a permitted class.

It seems that I would have to convert the tbl_df into a data.frame, in order for this work…

setClass("TestObject",
  slots = list(test.mat = "matrix",
               test.df = "data.frame"))

slot(test.object, "test.df") <- as.data.frame(flights)
## [1] "data.frame"

But doing this loses the benefits of having it as a tbl_df (e.g. print a few rows, all the columns fit on one screen, etc)

class(slot(test.object, "test.df"))
head(slot(test.object, "test.df"))
##   year month day dep_time dep_delay arr_time arr_delay carrier tailnum
## 1 2013     1   1      517         2      830        11      UA  N14228
## 2 2013     1   1      533         4      850        20      UA  N24211
## 3 2013     1   1      542         2      923        33      AA  N619AA
## 4 2013     1   1      544        -1     1004       -18      B6  N804JB
## 5 2013     1   1      554        -6      812       -25      DL  N668DN
## 6 2013     1   1      554        -4      740        12      UA  N39463
##   flight origin dest air_time distance hour minute
## 1   1545    EWR  IAH      227     1400    5     17
## 2   1714    LGA  IAH      227     1416    5     33
## 3   1141    JFK  MIA      160     1089    5     42
## 4    725    JFK  BQN      183     1576    5     44
## 5    461    LGA  ATL      116      762    5     54
## 6   1696    EWR  ORD      150      719    5     54

I have a feeling I am not understanding how tbl_df (perhaps it's not a class). But, is there a way to get a tbl_df "class" to work with S4 and the data.frame permitted class?

The full code can be found here:

https://github.com/tinyheero/tbl-df-s4/blob/master/tbl-df-s4.Rmd

And corresponding html:

https://htmlpreview.github.io/?https://github.com/tinyheero/tbl-df-s4/blob/master/tbl-df-s4.html

R Session Details

devtools::session_info()
## Session info --------------------------------------------------------------
##  setting  value                       
##  version  R version 3.2.2 (2015-08-14)
##  system   x86_64, darwin14.5.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_CA.UTF-8                 
##  tz       America/Vancouver           
##  date     2016-02-25
## Packages ------------------------------------------------------------------
##  package      * version date       source                            
##  assertthat     0.1     2013-12-06 CRAN (R 3.2.2)                    
##  DBI            0.3.1   2014-09-24 CRAN (R 3.2.2)                    
##  devtools       1.10.0  2016-01-23 CRAN (R 3.2.2)                    
##  digest         0.6.9   2016-01-08 CRAN (R 3.2.2)                    
##  dplyr        * 0.4.3   2015-09-01 CRAN (R 3.2.2)                    
##  evaluate       0.8     2015-09-18 CRAN (R 3.2.2)                    
##  formatR        1.2.1   2015-09-18 CRAN (R 3.2.2)                    
##  htmltools      0.3     2015-12-29 CRAN (R 3.2.2)                    
##  knitr        * 1.12.3  2016-01-22 CRAN (R 3.2.2)                    
##  magrittr       1.5     2014-11-22 CRAN (R 3.2.2)                    
##  memoise        0.2.1   2014-04-22 CRAN (R 3.2.2)                    
##  nycflights13 * 0.1     2014-07-22 CRAN (R 3.2.2)                    
##  R6             2.1.2   2016-01-26 CRAN (R 3.2.2)                    
##  Rcpp           0.12.3  2016-01-10 CRAN (R 3.2.2)                    
##  rmarkdown      0.9.5   2016-02-12 Github (rstudio/rmarkdown@b24f7e7)
##  stringi        1.0-1   2015-10-22 CRAN (R 3.2.2)                    
##  stringr        1.0.0   2015-04-30 CRAN (R 3.2.2)                    
##  yaml           2.1.13  2014-06-12 CRAN (R 3.2.2)

Many thanks in advance.


Solution

  • From S4s perspective it is not clear that tbl_df is really a data.frame. You can declare that relationship by using setOldClass where you have to provide the full list of class names of a tbl_df object. Doing this explains to S4 that 'tbl_df' really inherits from data.frame:

    setOldClass(c("tbl_df", "tbl", "data.frame"))