在R中作为OO编程的第一次尝试时,我声明了一个S4对象TestObject,它具有两个插槽,其中一个插槽(test.df)具有允许的data.frame类:

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

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

令我惊讶的是,当我尝试分配tbl_df“class”的对象时:
library("nycflights13")
class(flights)
## [1] "tbl_df"     "tbl"        "data.frame"

它抛出一个错误:
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")

我的印象是tbl_df只是包裹在data.frame周围。我以为也许可以将tbl_df声明为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")

但是它似乎没有将其识别为允许的类。

看来我必须将tbl_df转换为data.frame才能完成这项工作……
setClass("TestObject",
  slots = list(test.mat = "matrix",
               test.df = "data.frame"))

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

但是这样做会失去将其作为tbl_df的好处(例如,打印几行,所有列都适合一个屏幕,等等)
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

我有一种不了解tbl_df的感觉(也许这不是一个类)。但是,是否有一种方法可以使tbl_df“类”与S4和data.frame允许的类一起使用?

完整的代码可以在这里找到:

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

和相应的html:

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

R session 详情
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)

提前谢谢了。

最佳答案

从S4s的角度来看,tbl_df是否真的是data.frame尚不清楚。您可以使用setOldClass声明这种关系,其中您必须提供tbl_df对象的类名的完整列表。这样做向S4解释了'tbl_df'实际上是从data.frame继承的:

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

关于r - 带S4对象插槽的tbl_df,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35642191/

10-12 14:01