Skip to content

Commit

Permalink
Better subsetting for tbls.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Sep 22, 2014
1 parent cc8d306 commit e647288
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2 (4.0.2): do not edit by hand

S3method("[",grouped_df)
S3method("[",tbl_df)
S3method(all.equal,tbl_df)
S3method(all.equal,tbl_dt)
S3method(anti_join,data.frame)
Expand Down
39 changes: 39 additions & 0 deletions R/tbl-df.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
#' tbl objects only print a few rows and all the columns that fit on one
#' screen, describing the rest of it as text.
#'
#' @section Methods:
#'
#' \code{tbl_df} implements two important base methods:
#'
#' \describe{
#' \item{print}{Only prints the first 10 rows, and the columns that fit on
#' screen}
#' \item{\code{[}}{Never simplifies (drops), so always returns data.frame}
#' }
#'
#' @export
#' @param data a data frame
#' @examples
Expand Down Expand Up @@ -93,3 +103,32 @@ print.tbl_df <- function(x, ..., n = NULL) {
trunc_mat(x, n = n)
}

#' @export
`[.tbl_df` <- function (x, i, j, drop = FALSE) {
if (missing(i) && missing(j)) return(x)
if (drop) warning("drop ignored", call. = FALSE)

# Escape early if nargs() == 2L; ie, column subsetting
if (nargs() == 2L) {
result <- .subset(x, i)
class(result) <- c("tbl_df", "data.frame")
attr(result, "row.names") <- .set_row_names(length(x[[1]]))
return(result)
}

# First, subset columns
if (!missing(j)) {
x <- .subset(x, j)
}

# Next, subset rows
if (!missing(i)) {
x <- lapply(x, `[`, i)
}

# TODO: handle 0 column case
class(x) <- c("tbl_df", "data.frame")
attr(x, "row.names") <- .set_row_names(length(x[[1]]))
x
}

11 changes: 11 additions & 0 deletions man/tbl_df.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ a \code{tbl_df} over a regular data frame is the printing:
tbl objects only print a few rows and all the columns that fit on one
screen, describing the rest of it as text.
}
\section{Methods}{


\code{tbl_df} implements two important base methods:

\describe{
\item{print}{Only prints the first 10 rows, and the columns that fit on
screen}
\item{\code{[}}{Never simplifies (drops), so always returns data.frame}
}
}
\examples{
ds <- tbl_df(mtcars)
ds
Expand Down
9 changes: 8 additions & 1 deletion tests/testthat/test-tbl-df.r
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ context("tbl_df/grouped_df")
test_that("grouped_df returns tbl_df if no groups", {
df <- grouped_df(mtcars, list())
expect_equal(class(df), c("tbl_df", "tbl", "data.frame"))
})
})

test_that("[ never drops", {
mtcars2 <- tbl_df(mtcars)
expect_is(mtcars2[, 1], "data.frame")
expect_is(mtcars2[, 1], "tbl_df")
expect_equal(mtcars2[, 1], mtcars2[1])
})

0 comments on commit e647288

Please sign in to comment.