From 9e3e64ebd6cc4b6e9cc3d60f104175d7dde7a4e2 Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 22 Feb 2024 14:52:39 -0500 Subject: [PATCH] Make more helpful warning for data_color. --- NEWS.md | 2 ++ R/data_color.R | 23 +++++++++++++++++------ tests/testthat/_snaps/data_color.md | 11 +++++++++++ tests/testthat/test-data_color.R | 14 ++++++++++++++ tests/testthat/test-fmt_markdown.R | 2 +- 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1af3a41e46..08df18b657 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # gt (development version) +* `data_color()` throws a more informative error if a calculation failed (@olivroy, #1373). + # gt 0.10.1 ## Improvements to nanoplots diff --git a/R/data_color.R b/R/data_color.R index b1d4f7f75a..a67c4e12de 100644 --- a/R/data_color.R +++ b/R/data_color.R @@ -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 diff --git a/tests/testthat/_snaps/data_color.md b/tests/testthat/_snaps/data_color.md index 22b24b0269..fb3b85a101 100644 --- a/tests/testthat/_snaps/data_color.md +++ b/tests/testthat/_snaps/data_color.md @@ -250,3 +250,14 @@ Output [1] "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n\n\n\n\n\n\n\n\n\n \n \n \n
2013201420152016201720182019202020212022
Mongolia2845153290282329647493029555309603031639913232430329433533477823398366
" +# 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` + diff --git a/tests/testthat/test-data_color.R b/tests/testthat/test-data_color.R index 5bcffee76b..790614b80d 100644 --- a/tests/testthat/test-data_color.R +++ b/tests/testthat/test-data_color.R @@ -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) + ) +}) diff --git a/tests/testthat/test-fmt_markdown.R b/tests/testthat/test-fmt_markdown.R index 53be75dd2f..eddc279a29 100644 --- a/tests/testthat/test-fmt_markdown.R +++ b/tests/testthat/test-fmt_markdown.R @@ -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() })