Skip to content
Draft
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
7 changes: 6 additions & 1 deletion R/dt_stub_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ dt_stub_df_init <- function(

# dplyr::recode is superseded, and is slower now.
# TODO consider using vctrs::vec_case_match when available r-lib/vctrs#1622
row_group_ids <- dplyr::recode(row_group_labels, !!!unique_row_group_ids)
# Handle empty dataframe case where unique_row_group_ids is empty
if (length(unique_row_group_ids) == 0) {
row_group_ids <- row_group_labels
} else {
row_group_ids <- dplyr::recode(row_group_labels, !!!unique_row_group_ids)
}

} else {
row_group_ids <- row_group_labels
Expand Down
31 changes: 31 additions & 0 deletions tests/testthat/test-gt_object.R
Original file line number Diff line number Diff line change
Expand Up @@ -992,3 +992,34 @@ test_that("Formatting functions operate with a 'last-one-wins' approach", {
)
)
})

test_that("Empty dataframes with process_md = TRUE work correctly (issue #2081)", {

# Test case 1: Empty dataframe with groupname_col and process_md = TRUE
# Should not throw "No replacements provided" error
expect_no_error(
mtcars[0, ] |> gt(groupname_col = "cyl", process_md = TRUE)
)

# Test case 2: Empty grouped dataframe with process_md = TRUE
# Should not throw "No replacements provided" error
expect_no_error(
mtcars[0, ] |> dplyr::group_by(cyl) |> gt(process_md = TRUE)
)

# Test case 3: Empty dataframe with rowname_col and process_md = TRUE
# Should continue to work as before
expect_no_error(
mtcars[0, ] |> gt(rowname_col = "cyl", process_md = TRUE)
)

# Test case 4: Regression test - non-empty dataframes should still work
expect_no_error(
mtcars[1:5, ] |> gt(groupname_col = "cyl", process_md = TRUE)
)

# Test case 5: Verify empty table is created with correct structure
empty_gt <- mtcars[0, ] |> gt(groupname_col = "cyl", process_md = TRUE)
expect_s3_class(empty_gt, "gt_tbl")
expect_equal(nrow(dt_data_get(empty_gt)), 0)
})