Skip to content

Commit

Permalink
Merge pull request #1585 from olivroy/error-msg
Browse files Browse the repository at this point in the history
Make more helpful error for `data_color()` with infinite values
  • Loading branch information
rich-iannone authored Feb 28, 2024
2 parents b482356 + daabcb8 commit 46c5e97
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# gt (development version)

* `data_color()` throws a more informative error if a calculation failed (@olivroy, #1373).

* `gtsave()` saves correctly to .rtf if using `cols_label()` and `summary_rows()` or `grand_summary_rows()` (@olivroy, #1233)

# gt 0.10.1
Expand Down
23 changes: 17 additions & 6 deletions R/data_color.R
Original file line number Diff line number Diff line change
Expand Up @@ -942,15 +942,26 @@ data_color <- function(
if (is.numeric(data_vals)) {

# Create a color function based on `scales::col_numeric()`
# Rethrow the error if something occurs. #1373
color_fn <-
scales::col_numeric(
palette = palette,
domain = domain %||% data_vals,
na.color = na_color,
alpha = TRUE,
reverse = reverse
withCallingHandlers(
scales::col_numeric(
palette = palette,
domain = domain %||% data_vals,
na.color = na_color,
alpha = TRUE,
reverse = reverse
),
error = function(e) {
cli::cli_abort(c(
"Failed to compute colors for column {.code {resolved_columns[i]}}.",
i = "Did the column include infinite values?"),
parent = e
)
}
)


} else if (is.character(data_vals) || is.factor(data_vals)) {

# At the time of this writing, scales has a bug where palettes in the
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/_snaps/data_color.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,14 @@
Output
[1] "<table class=\"gt_table\" data-quarto-disable-processing=\"false\" data-quarto-bootstrap=\"false\">\n <thead>\n <tr class=\"gt_col_headings\">\n <th class=\"gt_col_heading gt_columns_bottom_border gt_left\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"\"></th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"2013\">2013</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"2014\">2014</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"2015\">2015</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"2016\">2016</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"2017\">2017</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"2018\">2018</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"2019\">2019</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"2020\">2020</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"2021\">2021</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"2022\">2022</th>\n </tr>\n </thead>\n <tbody class=\"gt_table_body\">\n <tr><th id=\"stub_1_1\" scope=\"row\" class=\"gt_row gt_left gt_stub\">Mongolia</th>\n<td headers=\"stub_1_1 2013\" class=\"gt_row gt_right\">2845153</td>\n<td headers=\"stub_1_1 2014\" class=\"gt_row gt_right\">2902823</td>\n<td headers=\"stub_1_1 2015\" class=\"gt_row gt_right\">2964749</td>\n<td headers=\"stub_1_1 2016\" class=\"gt_row gt_right\">3029555</td>\n<td headers=\"stub_1_1 2017\" class=\"gt_row gt_right\">3096030</td>\n<td headers=\"stub_1_1 2018\" class=\"gt_row gt_right\">3163991</td>\n<td headers=\"stub_1_1 2019\" class=\"gt_row gt_right\">3232430</td>\n<td headers=\"stub_1_1 2020\" class=\"gt_row gt_right\" style=\"background-color: #183212; color: #FFFFFF;\">3294335</td>\n<td headers=\"stub_1_1 2021\" class=\"gt_row gt_right\" style=\"background-color: #1D4515; color: #FFFFFF;\">3347782</td>\n<td headers=\"stub_1_1 2022\" class=\"gt_row gt_right\" style=\"background-color: #205A17; color: #FFFFFF;\">3398366</td></tr>\n </tbody>\n \n \n</table>"

# data_color errors gracefully when infinite values (#1373)

Code
data_color(gt_inf)
Condition
Error in `data_color()`:
! Failed to compute colors for column `y`.
i Did the column include infinite values?
Caused by error in `scales::col_numeric()`:
! Wasn't able to determine range of `domain`

14 changes: 14 additions & 0 deletions tests/testthat/test-data_color.R
Original file line number Diff line number Diff line change
Expand Up @@ -2055,3 +2055,17 @@ test_that("The `cell_fill()` function accepts colors of various types", {
unique() %>%
expect_equal("80")
})

test_that("data_color errors gracefully when infinite values (#1373)", {

d <- data.frame(
x = c(4, 1, 2, 3),
y = c(1, 2, 3, Inf)
)
gt_inf <- gt(d)

expect_snapshot(
error = TRUE,
data_color(gt_inf)
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-fmt_markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ test_that("LaTeX formulas render correctly in HTML", {
column_labels.border.lr.style = "solid",
column_labels.border.lr.width = px(1)
)

skip_if_not_installed("katex", "1.4.1")
# Take a snapshot of `gt_tbl`
gt_tbl %>% render_as_html() %>% expect_snapshot()
})

2 comments on commit 46c5e97

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.