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]. #'