Skip to content

Commit

Permalink
fixup cols_add() in corner cases
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Sep 26, 2024
1 parent 5873f80 commit 21a3791
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@

* `vec_fmt_*()` (and incidentally `cols_nanoplot()`) should be faster now (@olivroy, #1888, #1891).

* `cols_add()` works in more cases (#1893).

# gt 0.11.0

## New features
Expand Down
25 changes: 11 additions & 14 deletions R/cols_add.R
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,6 @@ cols_add <- function(
resolved_column_before <- NULL
} else if (length(resolved_column_before) != 1) {

if (length(resolved_column_before) < 1) {
cli::cli_abort("The expression used for `.before` did not resolve a column.")
}

if (length(resolved_column_before) > 1) {
cli::cli_abort("The expression used for `.before` resolved multiple columns.")
}
Expand All @@ -404,14 +400,15 @@ cols_add <- function(
resolved_column_after <- NULL
} else if (length(resolved_column_after) != 1) {

if (length(resolved_column_after) < 1) {
cli::cli_abort("The expression used for `.after` did not resolve a column.")
}

if (length(resolved_column_after) > 1) {
cli::cli_abort("The expression used for `.after` resolved multiple columns.")
}
}

if (length(resolved_column_after) == 1 && resolved_column_after == colnames(data_tbl)[NCOL(data_tbl)]) {
# if requesting the last column to add after, use NULL instead.
resolved_column_after <- NULL
}

# Stop function if expressions are given to both `.before` and `.after`
if (!is.null(resolved_column_before) && !is.null(resolved_column_after)) {
Expand Down Expand Up @@ -455,9 +452,9 @@ cols_add <- function(
} else {
updated_data_tbl <-
dplyr::bind_cols(
dplyr::select(data_tbl, 1:(before_colnum - 1)),
dplyr::select(data_tbl, 1:(dplyr::all_of(before_colnum) - 1)),
data_tbl_new_cols,
dplyr::select(data_tbl, before_colnum:ncol(data_tbl))
dplyr::select(data_tbl, dplyr::all_of(before_colnum):ncol(data_tbl))
)
}

Expand All @@ -473,7 +470,7 @@ cols_add <- function(

} else if (is.null(resolved_column_before) && !is.null(resolved_column_after)) {

if (resolved_column_after == nrow(data_tbl)) {
if (resolved_column_after == ncol(data_tbl)) {

updated_data_tbl <-
vctrs::vec_cbind(
Expand All @@ -493,22 +490,22 @@ cols_add <- function(

if (after_colnum >= ncol(data_tbl)) {
updated_data_tbl <-
dplyr::bind_cols(
vctrs::vec_cbind(
data_tbl,
data_tbl_new_cols
)
} else {
updated_data_tbl <-
dplyr::bind_cols(
dplyr::select(data_tbl, 1:after_colnum),
dplyr::select(data_tbl, 1:dplyr::all_of(after_colnum)),
data_tbl_new_cols,
dplyr::select(data_tbl, (after_colnum + 1):ncol(data_tbl))
)
}


after_colnum <- which(boxh_df[["var"]] == resolved_column_after)

updated_boxh_df <-
dplyr::bind_rows(
boxh_df[1:after_colnum, ],
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/_snaps/cols_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# cols_add() errors with bad input

Code
cols_add(tbl, x = 1, .after = 2, .before = 3)
Condition
Error in `cols_add()`:
! Expressions cannot be given to both `.before` and `.after`.
Code
cols_add(tbl, x = 1, .after = 15)
Condition
Error in `vars_select_eval()`:
! Can't select columns past the end.
i Location 15 doesn't exist.
i There are only 9 columns.
Code
cols_add(tbl, x = 1, .before = c(1, 2))
Condition
Error in `cols_add()`:
! The expression used for `.before` resolved multiple columns.
Code
cols_add(tbl, x = 1, .after = c(1, 2))
Condition
Error in `cols_add()`:
! The expression used for `.after` resolved multiple columns.

30 changes: 28 additions & 2 deletions tests/testthat/test-cols_add.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
test_that("cols_add() works", {
tbl <- gt(exibble)
expect_no_error(
cols_add(tbl, x = 1, .before = 3)
dat <- cols_add(tbl, x = 1, .before = 1)
)
expect_equal(dat$`_boxhead`$var[1], "x")

expect_no_error(
dat <- cols_add(tbl, x = 1, .before = 2)
)
expect_equal(dat$`_boxhead`$var[2], "x")
expect_no_error(
dat <- cols_add(tbl, x = 1, .after = 1)
)
expect_equal(dat$`_boxhead`$var[2], "x")
expect_no_error(
dat <- cols_add(tbl, x = 1, .after = dplyr::last_col())
)
})

test_that("cols_add() errors with bad input", {
tbl <- gt(exibble)

expect_snapshot(
error = TRUE, {
cols_add(tbl, x = 1, .after = 2, .before = 3)
cols_add(tbl, x = 1, .after = 15)
cols_add(tbl, x = 1, .before = c(1, 2))
cols_add(tbl, x = 1, .after = c(1, 2))

}
)
expect_equal(cols_add(), what)
})

0 comments on commit 21a3791

Please sign in to comment.