Skip to content

Commit

Permalink
Fall back to regular subsetting.
Browse files Browse the repository at this point in the history
`[[.tbl_df` now falls back to regular subsetting when used with anything other than a single string.

Fixes #29
  • Loading branch information
hadley committed Mar 17, 2016
1 parent 6cfb350 commit 3d99995
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/tbl-df.r
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ print.tbl_df <- function(x, ..., n = NULL, width = NULL) {

#' @export
`[[.tbl_df` <- function(x, i, exact = TRUE) {
if (is.character(i) && !(i %in% names(x)))
if (is.character(i) && length(i) == 1L && !(i %in% names(x)))
stop("Unknown name", call. = FALSE)
if (!exact) {
warning("exact ignored", call. = FALSE)
}

.subset2(x, i)
NextMethod()
}

#' @export
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-tbl-df.r
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ test_that("[[.tbl_df ignores exact argument",{
expect_warning(foo[["x", exact = FALSE]], "ignored")
expect_identical(getElement(foo, "y"), 1:10)
})

test_that("can use recursive indexing with [[", {
foo <- data_frame(x = list(y = 1:3, z = 4:5))
expect_equal(foo[[c(1, 1)]], 1:3)
expect_equal(foo[[c("x", "y")]], 1:3)
})

0 comments on commit 3d99995

Please sign in to comment.