From 04d3290e680e73051e835dd762e88cce51a9390c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Thu, 30 Jun 2016 16:44:06 +0200 Subject: [PATCH 1/2] don't check name in [[ --- R/tbl-df.r | 3 --- tests/testthat/test-tbl-df.R | 6 +++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/R/tbl-df.r b/R/tbl-df.r index 76121d000..583b91453 100644 --- a/R/tbl-df.r +++ b/R/tbl-df.r @@ -21,9 +21,6 @@ print.tbl_df <- function(x, ..., n = NULL, width = NULL) { colname <- i else colname <- j - if (is.character(colname) && length(colname) == 1L && !(colname %in% names(x))) { - stopc("Unknown column '", colname, "'") - } if (!exact) { warningc("exact ignored") } diff --git a/tests/testthat/test-tbl-df.R b/tests/testthat/test-tbl-df.R index 5416a78c0..8fcbafd9e 100644 --- a/tests/testthat/test-tbl-df.R +++ b/tests/testthat/test-tbl-df.R @@ -110,10 +110,10 @@ test_that("can use recursive indexing with [[", { expect_equal(foo[[c("x", "y")]], 1:3) }) -test_that("[[ throws error if name doesn't exist", { +test_that("[[ returns NULL if name doesn't exist", { df <- data_frame(x = 1) - expect_error(df[["y"]], "Unknown column 'y'") - expect_error(df[[1, "y"]], "Unknown column 'y'") + expect_null(df[["y"]]) + expect_null(df[[1, "y"]]) }) test_that("can use two-dimensional indexing with [[", { From 4c3dca30d1db7e3102909babb6f7dd07f029ef01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Thu, 30 Jun 2016 16:44:52 +0200 Subject: [PATCH 2/2] warn, and use exact = FALSE, for $ --- R/tbl-df.r | 7 ++++--- tests/testthat/test-tbl-df.R | 9 ++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/R/tbl-df.r b/R/tbl-df.r index 583b91453..510d49baf 100644 --- a/R/tbl-df.r +++ b/R/tbl-df.r @@ -31,10 +31,11 @@ print.tbl_df <- function(x, ..., n = NULL, width = NULL) { #' @export `$.tbl_df` <- function(x, i) { if (is.character(i) && !(i %in% names(x))) { - stopc("Unknown column '", i, "'") + warningc("Unknown column '", i, "'") + .subset2(x, i, exact = FALSE) + } else { + .subset2(x, i) } - - .subset2(x, i) } #' @export diff --git a/tests/testthat/test-tbl-df.R b/tests/testthat/test-tbl-df.R index 8fcbafd9e..9babd405f 100644 --- a/tests/testthat/test-tbl-df.R +++ b/tests/testthat/test-tbl-df.R @@ -126,13 +126,16 @@ test_that("can use two-dimensional indexing with [[", { test_that("$ throws error if name doesn't exist", { df <- data_frame(x = 1) - expect_error(df$y, "Unknown column 'y'") + expect_warning(expect_null(df$y), + "Unknown column 'y'") }) test_that("$ doesn't do partial matching", { df <- data_frame(partial = 1) - expect_error(df$p, "Unknown column 'p'") - expect_error(df$part, "Unknown column 'part'") + expect_warning(expect_identical(df$p, 1), + "Unknown column 'p'") + expect_warning(expect_identical(df$part, 1), + "Unknown column 'part'") expect_error(df$partial, NA) })