tidyfun / tf

S3 classes and methods for tidy functional data
https://tidyfun.github.io/tf/
GNU Affero General Public License v3.0
5 stars 2 forks source link

`tfd()` and `tfb()` print not formatting correctly #37

Open m-muecke opened 8 months ago

m-muecke commented 8 months ago

Spacing after the commas is not working for every position and perhaps a space after the semicolon makes reading easier.

tf::tf_rgp(1, arg = seq(0, 1, length.out = 3))
#> tfd[1] on (0,1) based on 3 evaluations each
#> interpolation by tf_approx_linear 
#> [1]: (0.0, 1.064);(0.5,-1.160);(1.0,-0.075)

Created on 2024-01-05 with reprex v2.0.2

fabian-s commented 8 months ago

not gonna do this now, but agree that a better print method would be worthwhile.

fabian-s commented 3 months ago

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] "▁▁██"

m-muecke commented 3 months ago

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

fabian-s commented 3 months ago

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 strand print and still be generally applicable, fast/cheap and (more) informative (than what we do now).

things to think about:

... what else?