Skip to content

Commit

Permalink
Merge pull request #3 from khusmann/dev
Browse files Browse the repository at this point in the history
bump to v0.2.1
  • Loading branch information
khusmann authored May 30, 2024
2 parents 35ea0af + 26907fd commit c07ebde
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"))
Description: Textual tabular data sources often encode values and missing
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 37 additions & 11 deletions R/interlaced.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 --------------------------------------------------------------
Expand Down

0 comments on commit c07ebde

Please sign in to comment.