实际上很难找到有关此主题的信息。 tbl/is.tbl 的文档提供的信息很少。
据我所知,tbl 是 dplyr 函数作为数据参数接收的表格数据的通用类。
创建 tbl 会将“tbl_”添加到类名中。来自dplyr/tbl.r:
#' Create a "tbl" object
#'
#' `tbl()` is the standard constructor for tbls. `as.tbl()` coerces,
#' and `is.tbl()` tests.
#'
#' @keywords internal
#' @export
#' @param subclass name of subclass. "tbl" is an abstract base class, so you
#' must supply this value. `tbl_` is automatically prepended to the
#' class name
#' @param object to test/coerce.
#' @param ... For `tbl()`, other fields used by class. For `as.tbl()`,
#' other arguments passed to methods.
#' @examples
#' as.tbl(mtcars)
make_tbl <- function(subclass, ...) {
subclass <- paste0("tbl_", subclass)
structure(list(...), class = c(subclass, "tbl"))
在 data.frame 上使用任何 dplyr 函数(join、select、mutate 等)都会返回一个 data.frame。
library(dplyr)
select(mtcars, cyl) %>% class
但是,在 tbl_df/tibble 上调用 dplyr 函数(请参阅 my answer on tibble vs tbl_df)会返回 tbl_df/tibble。
> select(tbl_df(mtcars), cyl) %>% class
[1] "tbl_df" "tbl" "data.frame"
这里我们看到tbl_df继承自tbl,后者继承自data.frame。