Skip to content

Commit

Permalink
Deprecate .data
Browse files Browse the repository at this point in the history
Fixes #169
  • Loading branch information
hadley committed Aug 16, 2022
1 parent f1d7c5c commit bf2ac30
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
@@ -1,5 +1,9 @@
# tidyselect (development version)

* Use of `.data` in tidyselect expressions is now deprecated to more cleanly
separate tidy-select from data-masking. Use `any_of()` or `all_of()` instead
(#169).

* `peek_var()` error now generates hyperlink to docs with recent RStudio (#289).

* `all_of()` (like `any_of()`) now fails when used outside of a
Expand Down
5 changes: 5 additions & 0 deletions R/eval-walk.R
Expand Up @@ -271,6 +271,11 @@ call_kind <- function(expr, error_call) {
fn <- as_string(head)

if (fn %in% c("$", "[[") && identical(node_cadr(expr), quote(.data))) {
lifecycle::deprecate_warn("1.2.0",
what = I("Use of .data in tidyselect expressions"),
with = I("`any_of()` or `all_of()`")
)

validate_dot_data(expr, error_call)
return(".data")
}
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/eval-walk.md
Expand Up @@ -76,6 +76,15 @@
Caused by error:
! object 'foobar' not found

# use of .data is deprecated

Code
x <- select_loc(x, .data$a)
Condition
Warning:
Use of .data in tidyselect expressions was deprecated in tidyselect 1.2.0.
Please use `any_of()` or `all_of()` instead.

# eval_walk() has informative messages

Code
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-eval-walk.R
Expand Up @@ -269,12 +269,19 @@ test_that("selections provide informative errors", {
})

test_that("can select with .data pronoun (#2715)", {
withr::local_options(lifecycle_verbosity = "quiet")

expect_identical(select_loc(c(foo = "foo"), .data$foo), c(foo = 1L))
expect_identical(select_loc(c(foo = "foo"), .data[["foo"]]), c(foo = 1L))
expect_identical(select_loc(letters2, .data$a:.data$b), c(a = 1L, b = 2L))
expect_identical(select_loc(letters2, .data[["a"]]:.data[["b"]]), c(a = 1L, b = 2L))
})

test_that("use of .data is deprecated", {
x <- list(a = 1, b = 2, c = 3)
expect_snapshot(x <- select_loc(x, .data$a))
})

test_that(".data in env-expression has the lexical definition", {
quo <- local({
.data <- mtcars
Expand Down Expand Up @@ -327,6 +334,8 @@ test_that("eval_sym() still supports predicate functions starting with `is`", {
})

test_that("eval_walk() has informative messages", {
withr::local_options(lifecycle_verbosity = "quiet")

expect_snapshot({
"# Using a predicate without where() warns"
invisible(select_loc(iris, is_integer))
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-lifecycle-deprecated.R
Expand Up @@ -326,6 +326,8 @@ test_that("vars_rename() sets variable context", {
})

test_that("vars_rename() supports `.data` pronoun", {
withr::local_options(lifecycle_verbosity = "quiet")

expect_identical(vars_rename(c("a", "b"), B = .data$b), c(a = "a", B = "b"))
})

Expand Down

0 comments on commit bf2ac30

Please sign in to comment.