S4 class from readr read_csv output

656 views Asked by At

I wanted to create an S4 class which represents the data from a read_csv function call (readr package)

library(readr)
library(magrittr)

#data <- read_csv("random.csv")

data <- structure(list(id = c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
                              10L, 10L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 30L, 
                              30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L),
                       value = c(0.711074015, 
                                0.614819585, 0.791768651, 0.385054413, 0.658395941, 0.204337366, 
                                  0.800191712, 0.049692407, 0.106693474, 0.989649574, 0.622873403, 
                                  0.269687142, 0.705086413, 0.520805849, 0.951492967, 0.63948476, 
                                  0.691096167, 0.284000329, 0.873882314, 0.48240776, 0.156761559, 
                                  0.149020867, 0.054223854, 0.429401826, 0.973400059, 0.030492575, 
                                  0.084345713, 0.538730795, 0.100815694, 0.443863626)), 
                  class = c("tbl_df","tbl", "data.frame"), 
                  row.names = c(NA, -30L), .Names = c("id","value"))

> head(data)
Source: local data frame [6 x 2]

     id     value
  (int)     (dbl)
1    10 0.7110740
2    10 0.6148196
3    10 0.7917687
4    10 0.3850544
5    10 0.6583959
6    10 0.2043374

I tried the following basic class setup

setClass(
      Class="RandomSample",
      slots=c(data="data.frame"),
      contains=c("data.frame")
    )


createContainer <- function(myData)
{
  return(new(Class = "RandomSample",data = myData))
}

containerBase <- createContainer(data)

which throws an error

Error in validObject(.Object) : 
  invalid class “RandomSample” object: 1: invalid object for slot "data" in class "RandomSample": got class "tbl_df", should be or extend class "data.frame"
invalid class “RandomSample” object: 2: invalid object for slot "data" in class "RandomSample": got class "tbl", should be or extend class "data.frame"
invalid class “RandomSample” object: 3: invalid object for slot "data" in class "RandomSample": got class "data.frame", should be or extend class "data.frame" 

I realize that the object created by read_csv is not an S4 class and has three objects data.frame tbl_df and tbl where tbl_df is a function object for printing and tbl is a generic method as described in the help.

So how do I define the class RandomSample as an S4 class which represents the read_csv output object?

1

There are 1 answers

0
andrnev On

The issue has been resolved by installing the latest version of the readr package (1.0.0). The read_csv function now adds additional attributes to the object which works when pulling into an S4 class. Don't fully understand why but learning slowly.