diff --git a/R/cols_add.R b/R/cols_add.R index 06506c5be..5f8ddedb7 100644 --- a/R/cols_add.R +++ b/R/cols_add.R @@ -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* #' @@ -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) @@ -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)) { @@ -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 <- @@ -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, ], diff --git a/R/cols_align_decimal.R b/R/cols_align_decimal.R index 4dbf71a51..0072995b0 100644 --- a/R/cols_align_decimal.R +++ b/R/cols_align_decimal.R @@ -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* #' @@ -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) @@ -255,3 +261,4 @@ align_to_char <- function(x, align_at = ".") { x_str } + diff --git a/man/cols_add.Rd b/man/cols_add.Rd index ff762791e..28ab74707 100644 --- a/man/cols_add.Rd +++ b/man/cols_add.Rd @@ -7,12 +7,19 @@ cols_add(.data, ..., .before = NULL, .after = NULL) } \arguments{ -\item{.data}{\emph{The gt table data object} +\item{.data}{\emph{The gt table or gt group data object} \verb{obj:} // \strong{required} This is the \strong{gt} table object that is commonly created through use of the -\code{\link[=gt]{gt()}} function.} +\code{\link[=gt]{gt()}} function. + +OR + +\verb{obj:} // \strong{required} + +This is the \strong{gt} group object that is commonly created through use of the +\code{\link[=gt_group]{gt_group()}} function.} \item{...}{\emph{Cell data assignments} diff --git a/man/cols_align_decimal.Rd b/man/cols_align_decimal.Rd index d9cacdabf..774c8245d 100644 --- a/man/cols_align_decimal.Rd +++ b/man/cols_align_decimal.Rd @@ -7,12 +7,19 @@ cols_align_decimal(data, columns = everything(), dec_mark = ".", locale = NULL) } \arguments{ -\item{data}{\emph{The gt table data object} +\item{data}{\emph{The gt table or gt group data object} \verb{obj:} // \strong{required} This is the \strong{gt} table object that is commonly created through use of the -\code{\link[=gt]{gt()}} function.} +\code{\link[=gt]{gt()}} function. + +OR + +\verb{obj:} // \strong{required} + +This is the \strong{gt} group object that is commonly created through use of the +\code{\link[=gt_group]{gt_group()}} function.} \item{columns}{\emph{Columns to target} diff --git a/tests/testthat/test-cols_add.R b/tests/testthat/test-cols_add.R index a2a87be17..6b9622592 100644 --- a/tests/testthat/test-cols_add.R +++ b/tests/testthat/test-cols_add.R @@ -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)) + +}) diff --git a/tests/testthat/test-cols_align_decimal.R b/tests/testthat/test-cols_align_decimal.R index 25c0f3ad1..75a7354aa 100644 --- a/tests/testthat/test-cols_align_decimal.R +++ b/tests/testthat/test-cols_align_decimal.R @@ -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)) + +})