From dc39397d6482eda0cc280f99dd2e19d2b5f25456 Mon Sep 17 00:00:00 2001 From: ryanzomorrodi Date: Fri, 28 Jun 2024 09:32:15 -0500 Subject: [PATCH 1/6] make NAs show up red in glimpse --- R/glimpse.R | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/R/glimpse.R b/R/glimpse.R index bcbd6ecf0..9be1b6280 100644 --- a/R/glimpse.R +++ b/R/glimpse.R @@ -144,7 +144,10 @@ format_glimpse.default <- function(x, ...) { dims_out <- paste0(dims, collapse = " x ") paste0("<", class(x)[[1]], "[", dims_out, "]>") } else { - format(x, trim = TRUE, justify = "none") + out <- format(x, trim = TRUE, justify = "none") + out[is.na(x)] <- crayon_red(NA) + + out } } @@ -169,14 +172,20 @@ format_glimpse.list <- function(x, ..., .inner = FALSE) { #' @export format_glimpse.character <- function(x, ...) { - encodeString(x, quote = '"') + out <- encodeString(as.character(x), quote = '"') + out[is.na(x)] <- crayon_red(NA) + + out } #' @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") } + out[is.na(x)] <- crayon_red(NA) + + out } From 88e74d29735c95ddca061df3b3155a10ff65cc63 Mon Sep 17 00:00:00 2001 From: ryanzomorrodi Date: Sat, 29 Jun 2024 14:28:09 -0500 Subject: [PATCH 2/6] test red missing values in glimpse --- tests/testthat/_snaps/glimpse.md | 69 ++++++++++++++++++++++++++++++++ tests/testthat/test-glimpse.R | 22 ++++++++++ 2 files changed, 91 insertions(+) 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..87d24e673 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)) + }) +}) \ No newline at end of file From 373a831ee7ed13f25ed70d6ecbd6ea3be83dc36c Mon Sep 17 00:00:00 2001 From: ryanzomorrodi Date: Sat, 29 Jun 2024 14:40:23 -0500 Subject: [PATCH 3/6] add style_na_if to style vectors --- R/glimpse.R | 12 +++--------- R/styles.R | 5 +++++ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/R/glimpse.R b/R/glimpse.R index 9be1b6280..654fb9a53 100644 --- a/R/glimpse.R +++ b/R/glimpse.R @@ -145,9 +145,7 @@ format_glimpse.default <- function(x, ...) { paste0("<", class(x)[[1]], "[", dims_out, "]>") } else { out <- format(x, trim = TRUE, justify = "none") - out[is.na(x)] <- crayon_red(NA) - - out + style_na_if(out, is.na(x)) } } @@ -173,9 +171,7 @@ format_glimpse.list <- function(x, ..., .inner = FALSE) { #' @export format_glimpse.character <- function(x, ...) { out <- encodeString(as.character(x), quote = '"') - out[is.na(x)] <- crayon_red(NA) - - out + style_na_if(out, is.na(x)) } #' @export @@ -185,7 +181,5 @@ format_glimpse.factor <- function(x, ...) { } else { out <- format(x, trim = TRUE, justify = "none") } - out[is.na(x)] <- crayon_red(NA) - - out + style_na_if(out, is.na(x)) } diff --git a/R/styles.R b/R/styles.R index c84fb2c78..b70fd8c23 100644 --- a/R/styles.R +++ b/R/styles.R @@ -84,6 +84,11 @@ style_na <- function(x) { crayon_red(x) } +style_na_if <- function(x, p) { + x[p] <- style_na("NA") + x +} + #' @details #' `style_neg()` is affected by the `pillar.neg` [option][pillar_options]. #' From 4c3f5a946ab22e0c7eb429e9d5c40f8ded20b66b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Mon, 1 Jul 2024 20:24:21 +0200 Subject: [PATCH 4/6] EOF --- tests/testthat/test-glimpse.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-glimpse.R b/tests/testthat/test-glimpse.R index 87d24e673..cfbf04cad 100644 --- a/tests/testthat/test-glimpse.R +++ b/tests/testthat/test-glimpse.R @@ -121,4 +121,4 @@ test_that("color test for missing values", { glimpse(df_all) glimpse(as.data.frame(df_all)) }) -}) \ No newline at end of file +}) From b51c9066af2cd07ced9942b01059e58e6847f96d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Mon, 1 Jul 2024 20:24:47 +0200 Subject: [PATCH 5/6] Reverse --- R/glimpse.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/glimpse.R b/R/glimpse.R index 654fb9a53..75c1b5030 100644 --- a/R/glimpse.R +++ b/R/glimpse.R @@ -140,12 +140,12 @@ format_glimpse_ <- function(x, ...) { format_glimpse.default <- function(x, ...) { dims <- dim(x) - if (!is.null(dims)) { - dims_out <- paste0(dims, collapse = " x ") - paste0("<", class(x)[[1]], "[", dims_out, "]>") - } else { + 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, "]>") } } From c6217ca7c57b94599fa8bdd9419ccf6f302f77e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Mon, 1 Jul 2024 20:32:03 +0200 Subject: [PATCH 6/6] Keep original formatting in x --- R/styles.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/R/styles.R b/R/styles.R index b70fd8c23..c5f138a95 100644 --- a/R/styles.R +++ b/R/styles.R @@ -85,7 +85,11 @@ style_na <- function(x) { } style_na_if <- function(x, p) { - x[p] <- style_na("NA") + idx <- which(p) + if (length(idx) == 0) { + return(x) + } + x[p] <- style_na(x[idx[[1]]]) x }