Skip to content
Closed
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
18 changes: 12 additions & 6 deletions R/cols_add.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#' empty (i.e., all `NA`) columns need to be added, you can use any of the `NA`
#' types (e.g., `NA`, `NA_character_`, `NA_real_`, etc.) for such columns.
#'
#' @inheritParams fmt_number
#' @inheritParams cols_align
#'
#' @param ... *Cell data assignments*
#'
Expand Down Expand Up @@ -226,7 +226,13 @@ cols_add <- function(
) {

# Perform input object validation
stop_if_not_gt_tbl(data = .data)
stop_if_not_gt_tbl_or_group(data = .data)

# Handle gt_group
if(inherits(.data, "gt_group")){
arg_list <- as.list(match.call())
return(apply_to_grp(.data, arg_list))
}

# Get the table's boxhead
boxh_df <- dt_boxhead_get(data = .data)
Expand Down Expand Up @@ -404,11 +410,11 @@ cols_add <- function(
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 @@ -441,7 +447,7 @@ cols_add <- function(
} else if (!is.null(resolved_column_before) && is.null(resolved_column_after)) {

before_colnum <- which(colnames(data_tbl) == resolved_column_before)

if (before_colnum <= 1) {
# put new column first
updated_data_tbl <-
Expand Down Expand Up @@ -480,7 +486,7 @@ cols_add <- function(
)

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

updated_boxh_df <-
vctrs::vec_rbind(
boxh_df[1:after_colnum, ],
Expand Down
11 changes: 9 additions & 2 deletions R/cols_align_decimal.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' number of columns (the function will skip over columns that don't require
#' this type of alignment).
#'
#' @inheritParams cols_hide
#' @inheritParams cols_align
#'
#' @param columns *Columns to target*
#'
Expand Down Expand Up @@ -113,7 +113,13 @@ cols_align_decimal <- function(
) {

# Perform input object validation
stop_if_not_gt_tbl(data = data)
stop_if_not_gt_tbl_or_group(data = data)

# Handle gt_group
if(inherits(data, "gt_group")){
arg_list <- as.list(match.call())
return(apply_to_grp(data, arg_list))
}

# Resolve the `locale` value here with the global locale value
locale <- resolve_locale(data = data, locale = locale)
Expand Down Expand Up @@ -255,3 +261,4 @@ align_to_char <- function(x, align_at = ".") {

x_str
}

11 changes: 9 additions & 2 deletions man/cols_add.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions man/cols_align_decimal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tests/testthat/test-cols_add.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,22 @@ test_that("cols_add() handles empty data frames", {
expect_named(tab$`_data`, c("x", "y", "z"))

})

test_that("check cols_add is applied gt_group", {

# Create a `gt_group` object of two `gt_tbl`s
# create gt group example
gt_tbl <- mtcars_short %>% gt()
gt_group <- gt_group(gt_tbl, gt_tbl)

# apply cols_add to table and group
add_gt_tbl <- gt_tbl %>%
cols_add(num = 1:5, char = rep("x",5))

add_gt_group <- gt_group %>%
cols_add(num = 1:5, char = rep("x",5))

# Expect identical if function applied before or after group is constructed
expect_identical(add_gt_group, gt_group(add_gt_tbl, add_gt_tbl))

})
30 changes: 30 additions & 0 deletions tests/testthat/test-cols_align_decimal.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,33 @@ test_that("Decimal alignment works in the basic case", {
# Perform snapshot test
expect_snapshot_html(gt_tbl_13)
})


test_that("check cols_align_decimal is applied gt_group", {

# Create a `gt_group` object of two `gt_tbl`s
# create gt group example
gt_tbl <- dplyr::tibble(
char = LETTERS[1:9],
num = c(1.2, -33.52, 9023.2, -283.527, NA, 0.401, -123.1, NA, 41)
) %>%
gt() %>%
fmt_number(
columns = num,
decimals = 3,
drop_trailing_zeros = TRUE
)

gt_group <- gt_group(gt_tbl, gt_tbl)

# apply alignment to table and group
aligned_gt_tbl <- gt_tbl %>%
cols_align_decimal()

aligned_gt_group <- gt_group %>%
cols_align_decimal()

# Expect identical if function applied before or after group is constructed
expect_identical(aligned_gt_group, gt_group(aligned_gt_tbl, aligned_gt_tbl))

})