Open m-muecke opened 8 months ago
not gonna do this now, but agree that a better print method would be worthwhile.
maybe: sparklines instead? (https://rosettacode.org/wiki/Sparkline_in_unicode)
bars <- intToUtf8(seq(0x2581, 0x2588), multiple = T) # ▁ ▂ ▃ ▄ ▅ ▆ ▇ █
n_chars <- length(bars)
sparkline <- function(numbers) {
mn <- min(numbers)
mx <- max(numbers)
interval <- mx - mn
bins <- sapply(
numbers,
function(i)
bars[[1 + min(n_chars - 1, floor((i - mn) / interval * n_chars))]]
)
sparkline <- paste0(bins, collapse = "")
return(sparkline)
}
sparkline(c(1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1))
sparkline(c(1.5, 0.5, 3.5, 2.5, 5.5, 4.5, 7.5, 6.5))
sparkline(c(0, 999, 4000, 4999, 7000, 7999))
sparkline(c(0, 0, 1, 1))
sparkline(c(0, 1, 19, 20))
[1] "▂▁▄▃▆▅█▇" [1] "▁▂▃▄▅▆▇█▇▆▅▄▃▂▁" [1] "▂▁▄▃▆▅█▇" [1] "▁▁▅▅██" [1] "▁▁██" [1] "▁▁██"
The sparkbar implementation from the cli package looks quite nice https://cli.r-lib.org/reference/spark_bar.html and the implementation is quite concise to copy and paste: https://github.com/r-lib/cli/blob/HEAD/R/spark.R#L64
Yeah, but since it's just like 20 lines I think we should copy/adjust (with attribution) and avoid the dependency.
Challenge I think will be to make this look good/show up in e.g. summary(<table>)
or glimpse(<table>)
as well as str
and print
and still be generally applicable, fast/cheap and (more) informative (than what we do now).
things to think about:
tf
?)tf
-vector? (see above re. comp. cost)... what else?
Spacing after the commas is not working for every position and perhaps a space after the semicolon makes reading easier.
Created on 2024-01-05 with reprex v2.0.2