diff --git a/R/glimpse.R b/R/glimpse.R index bcbd6ecf0..75c1b5030 100644 --- a/R/glimpse.R +++ b/R/glimpse.R @@ -140,11 +140,12 @@ format_glimpse_ <- function(x, ...) { format_glimpse.default <- function(x, ...) { dims <- dim(x) - if (!is.null(dims)) { + if (is.null(dims)) { + out <- format(x, trim = TRUE, justify = "none") + style_na_if(out, is.na(x)) + } else { dims_out <- paste0(dims, collapse = " x ") paste0("<", class(x)[[1]], "[", dims_out, "]>") - } else { - format(x, trim = TRUE, justify = "none") } } @@ -169,14 +170,16 @@ format_glimpse.list <- function(x, ..., .inner = FALSE) { #' @export format_glimpse.character <- function(x, ...) { - encodeString(x, quote = '"') + out <- encodeString(as.character(x), quote = '"') + style_na_if(out, is.na(x)) } #' @export format_glimpse.factor <- function(x, ...) { if (any(grepl(",", levels(x), fixed = TRUE))) { - encodeString(as.character(x), quote = '"') + out <- encodeString(as.character(x), quote = '"') } else { - format(x, trim = TRUE, justify = "none") + out <- format(x, trim = TRUE, justify = "none") } + style_na_if(out, is.na(x)) } diff --git a/R/styles.R b/R/styles.R index c84fb2c78..c5f138a95 100644 --- a/R/styles.R +++ b/R/styles.R @@ -84,6 +84,15 @@ style_na <- function(x) { crayon_red(x) } +style_na_if <- function(x, p) { + idx <- which(p) + if (length(idx) == 0) { + return(x) + } + x[p] <- style_na(x[idx[[1]]]) + x +} + #' @details #' `style_neg()` is affected by the `pillar.neg` [option][pillar_options]. #' diff --git a/tests/testthat/_snaps/glimpse.md b/tests/testthat/_snaps/glimpse.md index 809ac53e8..36872beda 100644 --- a/tests/testthat/_snaps/glimpse.md +++ b/tests/testthat/_snaps/glimpse.md @@ -216,3 +216,72 @@ $ cyl 6, 4, 8 $ data [], [], [] +# color test for missing values + + Code + # individual data types + format_glimpse(df_all$a) + Output + [1] "1.0" "2.5" "\033[31mNA\033[39m" + Code + format_glimpse(df_all$b) + Output + [1] "1" "2" "\033[31mNA\033[39m" + Code + format_glimpse(df_all$c) + Output + [1] "TRUE" "FALSE" "\033[31mNA\033[39m" + Code + format_glimpse(df_all$d) + Output + [1] "\"a\"" "\"b\"" "\033[31mNA\033[39m" + Code + format_glimpse(df_all$e) + Output + [1] "a" "b" "\033[31mNA\033[39m" + Code + format_glimpse(df_all$f) + Output + [1] "2015-12-10" "2015-12-11" "\033[31mNA\033[39m" + Code + format_glimpse(df_all$g) + Output + [1] "2015-12-09 10:51:35" "2015-12-09 10:51:36" "\033[31mNA\033[39m" + Code + format_glimpse(df_all$h) + Output + [1] "1" "2" "\033[31mNA\033[39m" + Code + format_glimpse(df_all$i) + Output + [1] "[1, <2, 3>]" "[<4, 5, 6>]" "[\033[31mNA\033[39m]" + Code + # tables + glimpse(df_all) + Output + Rows: 3 + Columns: 9 + $ a  1.0, 2.5, NA + $ b  1, 2, NA + $ c  TRUE, FALSE, NA + $ d  "a", "b", NA + $ e  a, b, NA + $ f  2015-12-10, 2015-12-11, NA + $ g  2015-12-09 10:51:35, 2015-12-09 10:51:36, NA + $ h  1, 2, NA + $ i  [1, <2, 3>], [<4, 5, 6>], [NA] + Code + glimpse(as.data.frame(df_all)) + Output + Rows: 3 + Columns: 9 + $ a  1.0, 2.5, NA + $ b  1, 2, NA + $ c  TRUE, FALSE, NA + $ d  "a", "b", NA + $ e  a, b, NA + $ f  2015-12-10, 2015-12-11, NA + $ g  2015-12-09 10:51:35, 2015-12-09 10:51:36, NA + $ h  1, 2, NA + $ i  [1, <2, 3>], [<4, 5, 6>], [NA] + diff --git a/tests/testthat/test-glimpse.R b/tests/testthat/test-glimpse.R index accab0164..cfbf04cad 100644 --- a/tests/testthat/test-glimpse.R +++ b/tests/testthat/test-glimpse.R @@ -100,3 +100,25 @@ test_that("output test for glimpse()", { glimpse(nested_mtcars_tbl, width = 70L) }) }) + +test_that("color test for missing values", { + local_colors() + + expect_snapshot({ + + "individual data types" + format_glimpse(df_all$a) + format_glimpse(df_all$b) + format_glimpse(df_all$c) + format_glimpse(df_all$d) + format_glimpse(df_all$e) + format_glimpse(df_all$f) + format_glimpse(df_all$g) + format_glimpse(df_all$h) + format_glimpse(df_all$i) + + "tables" + glimpse(df_all) + glimpse(as.data.frame(df_all)) + }) +})