Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Deprecate data_frame_(), lst_(), frame_data(), as_tibble(validate = ), as_tibble(NULL), new_tibble(subclass = ), add_row() and add_column() for non-data-frame input, add_column() for input with non-unique names, and corner cases for tbl[[x]] #1515

Merged
merged 4 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export(view)
exportClasses(tbl_df)
import(rlang)
importFrom(lifecycle,deprecate_soft)
importFrom(lifecycle,deprecate_stop)
importFrom(lifecycle,deprecate_warn)
importFrom(lifecycle,expect_deprecated)
importFrom(magrittr,"%>%")
Expand Down
7 changes: 3 additions & 4 deletions R/add.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ add_row <- function(.data, ..., .before = NULL, .after = NULL) {
}

if (!is.data.frame(.data)) {
deprecate_warn("2.1.1", "add_row(.data = 'must be a data frame')")
deprecate_stop("2.1.1", "add_row(.data = 'must be a data frame')")
}

if (dots_n(...) == 0L) {
Expand Down Expand Up @@ -127,14 +127,13 @@ rbind_at <- function(old, new, pos) {
add_column <- function(.data, ..., .before = NULL, .after = NULL,
.name_repair = c("check_unique", "unique", "universal", "minimal")) {
if (!is.data.frame(.data)) {
deprecate_warn("2.1.1", "add_column(.data = 'must be a data frame')")
deprecate_stop("2.1.1", "add_column(.data = 'must be a data frame')")
}

if (has_length(.data) && (!is_named(.data) || anyDuplicated(names2(.data))) && missing(.name_repair)) {
deprecate_warn("3.0.0", "add_column(.data = 'must have unique names')",
deprecate_stop("3.0.0", "add_column(.data = 'must have unique names')",
details = 'Use `.name_repair = "minimal"`.'
)
.name_repair <- "minimal"
}

df <- tibble(..., .name_repair = .name_repair)
Expand Down
29 changes: 7 additions & 22 deletions R/as_tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ as_tibble.data.frame <- function(x, validate = NULL, ...,
.rows = NULL,
.name_repair = c("check_unique", "unique", "universal", "minimal"),
rownames = pkgconfig::get_config("tibble::rownames", NULL)) {
.name_repair <- compat_name_repair(.name_repair, validate, missing(.name_repair))
if (!is.null(validate)) {
deprecate_stop("2.0.0", "tibble::as_tibble(validate = )", "as_tibble(.name_repair =)")
}

old_rownames <- raw_rownames(x)
if (is.null(.rows)) {
Expand Down Expand Up @@ -98,7 +100,9 @@ as_tibble.data.frame <- function(x, validate = NULL, ...,
#' @rdname as_tibble
as_tibble.list <- function(x, validate = NULL, ..., .rows = NULL,
.name_repair = c("check_unique", "unique", "universal", "minimal")) {
.name_repair <- compat_name_repair(.name_repair, validate, missing(.name_repair))
if (!is.null(validate)) {
deprecate_stop("2.0.0", "tibble::as_tibble(validate = )", "as_tibble(.name_repair =)")
}

lst_to_tibble(x, .rows, .name_repair, col_lengths(x))
}
Expand All @@ -110,25 +114,6 @@ lst_to_tibble <- function(x, .rows, .name_repair, lengths = NULL, call = caller_
recycle_columns(x, .rows, lengths)
}

compat_name_repair <- function(.name_repair, validate, .missing_name_repair) {
if (is.null(validate)) return(.name_repair)


if (!.missing_name_repair) {
name_repair <- .name_repair
} else if (isTRUE(validate)) {
name_repair <- "check_unique"
} else {
name_repair <- "minimal"
}

deprecate_soft("2.0.0", "tibble::as_tibble(validate = )", "as_tibble(.name_repair =)",
env = foreign_caller_env()
)

name_repair
}

check_valid_cols <- function(x, pos = NULL, call = caller_env()) {
names_x <- names2(x)

Expand Down Expand Up @@ -259,7 +244,7 @@ as_tibble.table <- function(x, `_n` = "n", ..., n = `_n`, .name_repair = "check_
#' @usage \method{as_tibble}{NULL}(x, ...)
as_tibble.NULL <- function(x, ...) {
if (missing(x)) {
deprecate_soft("3.0.0", "as_tibble(x = 'can\\'t be missing')")
deprecate_stop("3.0.0", "as_tibble(x = 'can\\'t be missing')")
}

new_tibble(list(), nrow = 0)
Expand Down
14 changes: 3 additions & 11 deletions R/deprecated.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,17 @@ tibble_ <- function(xs) {
#' @export
#' @rdname deprecated
data_frame_ <- function(xs) {
deprecate_soft("2.0.0", "data_frame_()", "tibble()",
deprecate_stop("2.0.0", "data_frame_()", "tibble()",
details = '`tibble()` supports dynamic dots, see `?"dyn-dots"`.'
)

xs <- compat_lazy_dots(xs, caller_env())
tibble(!!!xs)
}

#' @export
#' @rdname deprecated
lst_ <- function(xs) {
deprecate_soft("2.0.0", "lst_()", "lst()",
deprecate_stop("2.0.0", "lst_()", "lst()",
details = '`lst()` supports dynamic dots, see `?"dyn-dots"`.'
)

xs <- compat_lazy_dots(xs, caller_env())
lst(!!!xs)
}

#' @description
Expand Down Expand Up @@ -81,9 +75,7 @@ as.tibble <- function(x, ...) {
#' @export
#' @rdname deprecated
frame_data <- function(...) {
deprecate_soft("2.0.0", "frame_data()", "tribble()")

tribble(...)
deprecate_stop("2.0.0", "frame_data()", "tribble()")
}

#' Name repair
Expand Down
3 changes: 1 addition & 2 deletions R/new.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
new_tibble <- function(x, ..., nrow = NULL, class = NULL, subclass = NULL) {
# For compatibility with tibble < 2.0.0
if (is.null(class) && !is.null(subclass)) {
deprecate_soft("2.0.0", "tibble::new_tibble(subclass = )", "new_tibble(class = )")
class <- subclass
deprecate_stop("2.0.0", "tibble::new_tibble(subclass = )", "new_tibble(class = )")
}

#' @section Construction:
Expand Down
6 changes: 2 additions & 4 deletions R/sub.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,10 @@ NULL

tbl_subset2 <- function(x, j, j_arg, call = caller_env()) {
if (is.matrix(j)) {
deprecate_soft("3.0.0", "tibble::`[[.tbl_df`(j = 'can\\'t be a matrix",
deprecate_stop("3.0.0", "tibble::`[[.tbl_df`(j = 'can\\'t be a matrix')",
details = "Recursive subsetting is deprecated for tibbles.",
env = foreign_caller_env()
)

return(as.matrix(x)[[j]])
}

if (is.object(j)) {
Expand All @@ -219,7 +217,7 @@ tbl_subset2 <- function(x, j, j_arg, call = caller_env()) {
vectbl_as_col_location2(j, length(x), j_arg = j_arg, call = call)
}
} else if (length(j) == 2L) {
deprecate_soft("3.0.0", "tibble::`[[.tbl_df`(j = 'can\\'t be a vector of length 2')",
deprecate_stop("3.0.0", "tibble::`[[.tbl_df`(j = 'can\\'t be a vector of length 2')",
details = "Recursive subsetting is deprecated for tibbles.",
env = foreign_caller_env()
)
Expand Down
1 change: 1 addition & 0 deletions R/tibble-package.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## usethis namespace: start
#' @importFrom lifecycle deprecate_soft
#' @importFrom lifecycle deprecate_warn
#' @importFrom lifecycle deprecate_stop
#' @importFrom lifecycle expect_deprecated
#' @importFrom pkgconfig set_config
#' @importFrom utils head tail
Expand Down
2 changes: 1 addition & 1 deletion revdep/cran.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## revdepcheck results

We checked 5 reverse dependencies, comparing R CMD check results across CRAN and dev versions of this package.
We checked 3 reverse dependencies, comparing R CMD check results across CRAN and dev versions of this package.

* We saw 2 new problems
* We failed to check 0 packages
Expand Down
10 changes: 5 additions & 5 deletions revdep/problems.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* GitHub: https://github.com/kiangkiangkiang/ggESDA
* Source code: https://github.com/cran/ggESDA
* Date/Publication: 2022-08-19 08:40:10 UTC
* Number of recursive dependencies: 206
* Number of recursive dependencies: 208

Run `revdepcheck::cloud_details(, "ggESDA")` for more info

Expand All @@ -29,7 +29,7 @@ Run `revdepcheck::cloud_details(, "ggESDA")` for more info
> data(Cardiological)
> ggInterval_index(Cardiological, aes(x = Syst))
Error in get(x, envir = ns, inherits = FALSE) :
object 'tbl_subset_row' not found
object 'tbl_subset_col' not found
Calls: ggInterval_index ... as.data.frame -> [ -> [.symbolic_tbl -> getFromNamespace -> get
Execution halted
```
Expand All @@ -41,7 +41,7 @@ Run `revdepcheck::cloud_details(, "ggESDA")` for more info
--- re-building ‘ggESDA.Rmd’ using rmarkdown
Quitting from lines 47-57 (ggESDA.Rmd)
Error: processing vignette 'ggESDA.Rmd' failed with diagnostics:
object 'tbl_subset_row' not found
object 'tbl_subset_col' not found
--- failed re-building ‘ggESDA.Rmd’

SUMMARY: processing the following file failed:
Expand Down Expand Up @@ -82,7 +82,7 @@ Run `revdepcheck::cloud_details(, "RSDA")` for more info
> data(abalone)
> res <- sym.pca(abalone, 'centers')
Error in get(x, envir = ns, inherits = FALSE) :
object 'tbl_subset_row' not found
object 'tbl_subset_col' not found
Calls: sym.pca ... %in% -> [ -> [.symbolic_tbl -> getFromNamespace -> get
Execution halted
```
Expand All @@ -94,7 +94,7 @@ Run `revdepcheck::cloud_details(, "RSDA")` for more info
--- re-building ‘introduction.Rmd’ using rmarkdown
Quitting from lines 60-64 (introduction.Rmd)
Error: processing vignette 'introduction.Rmd' failed with diagnostics:
object 'tbl_subset_row' not found
object 'tbl_subset_col' not found
--- failed re-building ‘introduction.Rmd’

SUMMARY: processing the following file failed:
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/vignette-invariants/invariants.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ df[[1:2]]

```r
tbl[[1:2]]
#> Warning: The `j` argument of `[[.tbl_df` can't be a vector of length 2 as of tibble 3.0.0.
#> Error:
#> ! The `j` argument of `[[.tbl_df` can't be a vector of length 2 as of tibble 3.0.0.
#> i Recursive subsetting is deprecated for tibbles.
#> [1] NA
```

</td></tr><tr style="vertical-align:top"><td>
Expand Down
78 changes: 24 additions & 54 deletions tests/testthat/test-as_tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -484,100 +484,70 @@ test_that("as_tibble_col() can convert atomic vectors to data frame", {
test_that("`validate` triggers deprecation message, but then works", {
scoped_lifecycle_warnings()

expect_tibble_abort(
expect_warning(
as_tibble(list(a = 1, "hi"), validate = TRUE),
"deprecated",
fixed = TRUE
),
abort_column_names_cannot_be_empty(2, repair_hint = TRUE)
expect_error(
as_tibble(list(a = 1, "hi"), validate = TRUE)
)

expect_warning(
df <- as_tibble(list(a = 1, "hi", a = 2), validate = FALSE),
expect_error(
as_tibble(list(a = 1, "hi", a = 2), validate = FALSE),
"deprecated",
fixed = TRUE
)
expect_identical(names(df), c("a", "", "a"))

df <- data.frame(a = 1, "hi", a = 2)
names(df) <- c("a", "", "a")
expect_warning(
df <- as_tibble(df, validate = FALSE),
"deprecated",
fixed = TRUE
expect_error(
as_tibble(df, validate = FALSE)
)
expect_identical(names(df), c("a", "", "a"))

df <- data.frame(a = 1, "hi")
names(df) <- c("a", "")
expect_tibble_abort(
expect_warning(
as_tibble(df, validate = TRUE),
"deprecated",
fixed = TRUE
),
abort_column_names_cannot_be_empty(2, repair_hint = TRUE)
expect_error(
as_tibble(df, validate = TRUE)
)
})

test_that("`validate` always raises lifecycle warning.", {
expect_deprecated(
expect_tibble_abort(
as_tibble(list(a = 1, "hi"), validate = TRUE, .name_repair = "check_unique"),
abort_column_names_cannot_be_empty(2, repair_hint = TRUE)
)
expect_error(
as_tibble(list(a = 1, "hi"), validate = TRUE, .name_repair = "check_unique")
)

expect_deprecated(
df <- as_tibble(list(a = 1, "hi", a = 2), validate = FALSE, .name_repair = "minimal")
expect_error(
as_tibble(list(a = 1, "hi", a = 2), validate = FALSE, .name_repair = "minimal")
)
expect_identical(names(df), c("a", "", "a"))

df <- data.frame(a = 1, "hi", a = 2)
names(df) <- c("a", "", "a")
expect_deprecated(
df <- as_tibble(df, validate = FALSE, .name_repair = "minimal")
expect_error(
as_tibble(df, validate = FALSE, .name_repair = "minimal")
)
expect_identical(names(df), c("a", "", "a"))

df <- data.frame(a = 1, "hi")
names(df) <- c("a", "")
expect_tibble_abort(
expect_deprecated(
as_tibble(df, validate = TRUE, .name_repair = "check_unique")
),
abort_column_names_cannot_be_empty(2, repair_hint = TRUE)
expect_error(
as_tibble(df, validate = TRUE, .name_repair = "check_unique")
)
})

test_that("Inconsistent `validate` and `.name_repair` used together raise a warning.", {
expect_deprecated(
expect_tibble_abort(
as_tibble(list(a = 1, "hi"), validate = FALSE, .name_repair = "check_unique"),
abort_column_names_cannot_be_empty(2, repair_hint = TRUE)
)
expect_error(
as_tibble(list(a = 1, "hi"), validate = FALSE, .name_repair = "check_unique")
)

expect_deprecated(
df <- as_tibble(list(a = 1, "hi", a = 2), validate = TRUE, .name_repair = "minimal")
expect_error(
as_tibble(list(a = 1, "hi", a = 2), validate = TRUE, .name_repair = "minimal")
)
expect_identical(names(df), c("a", "", "a"))

df <- data.frame(a = 1, "hi", a = 2)
names(df) <- c("a", "", "a")
expect_deprecated(
df <- as_tibble(df, validate = TRUE, .name_repair = "minimal")
expect_error(
as_tibble(df, validate = TRUE, .name_repair = "minimal")
)
expect_identical(names(df), c("a", "", "a"))

df <- data.frame(a = 1, "hi")
names(df) <- c("a", "")
expect_deprecated(
expect_tibble_abort(
as_tibble(df, validate = FALSE, .name_repair = "check_unique"),
abort_column_names_cannot_be_empty(2, repair_hint = TRUE)
)
expect_error(
as_tibble(df, validate = FALSE, .name_repair = "check_unique")
)
})

Expand Down
26 changes: 0 additions & 26 deletions tests/testthat/test-new.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
test_that("new_tibble() with deprecated subclass argument", {
scoped_lifecycle_silence()

tbl <- new_tibble(
data.frame(a = 1:3),
names = "b",
attr1 = "value1",
attr2 = 2,
nrow = 3,
subclass = "nt"
)

# Can't compare directly due to dplyr:::all.equal.tbl_df()
expect_identical(class(tbl), c("nt", "tbl_df", "tbl", "data.frame"))
expect_equal(
unclass(tbl),
structure(
list(b = 1:3),
attr1 = "value1",
attr2 = 2,
.Names = "b",
row.names = .set_row_names(3L)
)
)
})

test_that("new_tibble() with new class argument", {
tbl <- new_tibble(
data.frame(a = 1:3),
Expand Down
Loading