Skip to content

Commit

Permalink
universal warning (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Jan 9, 2017
1 parent 2734ca9 commit e4b4e74
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
8 changes: 1 addition & 7 deletions R/tbl-df.r
Expand Up @@ -31,13 +31,7 @@ print.tbl_df <- function(x, ..., n = NULL, width = NULL, n_extra = NULL) {
#' @export
`$.tbl_df` <- function(x, i) {
if (is.character(i) && !has_name(x, i)) {
# R uses `*tmp*` as argument name for partial updates of the form
# df$col[indexes] <- values
if (identical(substitute(x), quote(`*tmp*`))) {
warningc("Uninitialised column '", i, "', please create the column before populating parts of it.")
} else {
warningc("Unknown column '", i, "'.")
}
warningc("Unknown or uninitialised column: '", i, "'.")
}
.subset2(x, i)
}
Expand Down
18 changes: 10 additions & 8 deletions tests/testthat/test-tbl-df.R
Expand Up @@ -37,12 +37,14 @@ test_that("[ with 0 cols creates correct row names (#656)", {
})

test_that("[.tbl_df is careful about names (#1245)",{
z_msg <- "Unknown column: 'z'"

foo <- tibble(x = 1:10, y = 1:10)
expect_error(foo["z"], "Unknown column: 'z'", fixed = TRUE)
expect_error(foo[ c("x", "y", "z") ], "Unknown column: 'z'", fixed = TRUE)
expect_error(foo["z"], z_msg, fixed = TRUE)
expect_error(foo[ c("x", "y", "z") ], z_msg, fixed = TRUE)

expect_error(foo[, "z"], "Unknown column: 'z'", fixed = TRUE)
expect_error(foo[, c("x", "y", "z") ], "Unknown column: 'z'", fixed = TRUE)
expect_error(foo[, "z"], z_msg, fixed = TRUE)
expect_error(foo[, c("x", "y", "z") ], z_msg, fixed = TRUE)

expect_error(foo[as.matrix("x")], "matrix")
expect_error(foo[array("x", dim = c(1, 1, 1))], "array")
Expand Down Expand Up @@ -127,20 +129,20 @@ test_that("can use two-dimensional indexing with [[", {
test_that("$ throws warning if name doesn't exist", {
df <- tibble(x = 1)
expect_warning(expect_null(df$y),
"Unknown column 'y'")
"Unknown or uninitialised column: 'y'")
})

test_that("$ throws different warning if attempting a partial initialization (#199)", {
df <- tibble(x = 1)
expect_warning(df$y[1] <- 2, "Uninitialised column 'y'")
expect_warning(df$y[1] <- 2, "Unknown or uninitialised column: 'y'")
})

test_that("$ doesn't do partial matching", {
df <- tibble(partial = 1)
expect_warning(expect_null(df$p),
"Unknown column 'p'")
"Unknown or uninitialised column: 'p'")
expect_warning(expect_null(df$part),
"Unknown column 'part'")
"Unknown or uninitialised column: 'part'")
expect_error(df$partial, NA)
})

Expand Down

0 comments on commit e4b4e74

Please sign in to comment.