diff --git a/DESCRIPTION b/DESCRIPTION index ac047c9..8eb400c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: interlacer Title: Read Tabular Data With Interlaced Values And Missing Reasons -Version: 0.2.0 +Version: 0.2.1 Authors@R: person("Kyle", "Husmann", , "kdh38@psu.edu", role = c("aut", "cre")) Description: Textual tabular data sources often encode values and missing diff --git a/NAMESPACE b/NAMESPACE index e29cb15..9255dae 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -24,6 +24,7 @@ S3method(flatten_channels,data.frame) S3method(flatten_channels,default) S3method(flatten_channels,interlacer_interlaced) S3method(format,interlacer_interlaced) +S3method(format,interlacer_interlaced_pillar) S3method(format,interlacer_na_col_spec) S3method(is.empty,default) S3method(is.empty,interlacer_interlaced) diff --git a/R/interlaced.R b/R/interlaced.R index 6ddc8ee..c8c9bb7 100644 --- a/R/interlaced.R +++ b/R/interlaced.R @@ -248,6 +248,10 @@ flatten_channels.interlacer_interlaced <- function(x, ...) { v <- value_channel(x) m <- na_channel(x) + if (all(is.na(m))) { + return(v) + } + if (!(is.numeric(v) && is.numeric(m)) && !(is.factor(v) && is.factor(m))) { v <- as.character(v) m <- as.character(m) @@ -355,17 +359,39 @@ style_empty <- function(x) { #' @importFrom pillar pillar_shaft #' @export pillar_shaft.interlacer_interlaced <- function(x, ...) { - align <- if (is_character(x)) "left" else "right" - items <- map(x, function(i) { - if (is.empty(i)) { - return(style_empty(format(i))) - } - if (is.na(i)) { - return(pillar::style_na(format(i))) - } - format(i) - }) - pillar::new_pillar_shaft_simple(items, align = align) + v <- pillar_shaft(value_channel(x), ...) + m <- pillar_shaft(na_channel(x), ...) + + width <- max(attr(v, "width"), attr(m, "width")) + + if (!is.null(attr(v, "min_width")) || !is.null(attr(v, "min_width"))) { + min_width <- max(attr(v, "min_width"), attr(m, "min_width")) + } else { + min_width <- NULL + } + + pillar::new_pillar_shaft( + list( + v = v, + m = m, + empty = is.empty(x), + na = is.na(x) + ), + width = width, + min_width = min_width, + class = "interlacer_interlaced_pillar" + ) +} + +#' @export +format.interlacer_interlaced_pillar <- function(x, width, ...) { + out <- format(x$v, width, ...) + out_na <- ansi_strip(format(x$m, width, ...)) + + out[x$na] <- pillar::style_na(paste0("<", out_na[x$na], ">")) + out[x$empty] <- style_empty(paste0("<<", out_na[x$empty], ">>")) + + out } # Proxies --------------------------------------------------------------