vincentarelbundock / tinytable

Simple and Customizable Tables in `R`
https://vincentarelbundock.github.io/tinytable
GNU General Public License v3.0
211 stars 18 forks source link

Add fn in format_tt to tutorial #146

Closed kylebutts closed 9 months ago

kylebutts commented 9 months ago

Closes #144

library(tinytable)
thumbdrives <- data.frame(
  date_lookup = as.Date(c("2024-01-15", "2024-01-18", "2024-01-14", "2024-01-16")),
  price = c(18.49, 19.99, 24.99, 24.99),
  price_rank = c(1, 2, 3, 3),
  memory = c(16e9, 12e9, 10e9, 8e9),
  speed_benchmark = c(0.6, 0.73, 0.82, 0.99)
)

tt(thumbdrives) |>
  format_tt(j = 1, fn = scales::label_date("%e %b", locale = "fr")) |>
  format_tt(j = 2, fn = scales::label_currency()) |>
  format_tt(j = 3, fn = scales::label_ordinal()) |> 
  format_tt(j = 4, fn = scales::label_bytes()) |> 
  format_tt(j = 5, fn = scales::label_percent())
date_lookup price price_rank memory speed_benchmark
15 janv. \$18.49 1st 16 GB 60%
18 janv. \$19.99 2nd 12 GB 73%
14 janv. \$24.99 3rd 10 GB 82%
16 janv. \$24.99 3rd 8 GB 99%

Created on 2024-02-21 with reprex v2.1.0

vincentarelbundock commented 9 months ago

Oh yeah, this is awesome. Will essentially save me from implementing all these custom formatting options. Fantastic!

The website didn't build because LaTeX compilation failed. I think one of your columns includes dollar signs, so you probably need escape=TRUE one on of the format calls.

kylebutts commented 9 months ago

Hmm, I just tried that and I think escape doesn't work with custom functions

library(tinytable)
thumbdrives <- data.frame(
  date_lookup = as.Date(c("2024-01-15", "2024-01-18", "2024-01-14", "2024-01-16")),
  price = c(18.49, 19.99, 24.99, 24.99),
  price_rank = c(1, 2, 3, 3),
  memory = c(16e9, 12e9, 10e9, 8e9),
  speed_benchmark = c(0.6, 0.73, 0.82, 0.99)
)

tt(thumbdrives) |>
  format_tt(j = 2, fn = scales::label_currency(), escape = TRUE) 
vincentarelbundock commented 9 months ago

Weird. On the bus now so can't check, but does scale return a standard character or some weird type?

vincentarelbundock commented 9 months ago

Oh no! It's silly. The escape is applied before the fn, so we just need to invest the order of these code blocks:

https://github.com/vincentarelbundock/tinytable/blob/main/R%2Fformat_tt.R

vincentarelbundock commented 9 months ago

Lines 235 and 255

vincentarelbundock commented 9 months ago

Thanks a ton! Fixed and merged to main. (The last little tweak was to escape the column names as well, since they have underscores.)

I really like changes like this: a small addition to documentation that makes obsolete many many hours of work I had mentally prepared for. The most efficient kind of contribution!

kylebutts commented 9 months ago

I think the last format_tt(escape = TRUE) zaps previous formatting. From the vignette, that seems like unintended behavior?

library(tinytable)

thumbdrives <- data.frame(
  date_lookup = as.Date(c("2024-01-15", "2024-01-18", "2024-01-14", "2024-01-16")),
  price = c(18.49, 19.99, 24.99, 24.99),
  price_rank = c(1, 2, 3, 3),
  memory = c(16e9, 12e9, 10e9, 8e9),
  speed_benchmark = c(0.6, 0.73, 0.82, 0.99)
)

tt(thumbdrives) |>
  format_tt(j = 1, fn = scales::label_date("%e %b", locale = "fr")) |>
  format_tt(j = 2, fn = scales::label_currency()) |>
  format_tt(j = 3, fn = scales::label_ordinal()) |> 
  format_tt(j = 4, fn = scales::label_bytes()) |> 
  format_tt(j = 5, fn = scales::label_percent())
date_lookup price price_rank memory speed_benchmark
15 janv. \$18.49 1st 16 GB 60%
18 janv. \$19.99 2nd 12 GB 73%
14 janv. \$24.99 3rd 10 GB 82%
16 janv. \$24.99 3rd 8 GB 99%

tt(thumbdrives) |>
  format_tt(j = 1, fn = scales::label_date("%e %b", locale = "fr")) |>
  format_tt(j = 2, fn = scales::label_currency()) |>
  format_tt(j = 3, fn = scales::label_ordinal()) |> 
  format_tt(j = 4, fn = scales::label_bytes()) |> 
  format_tt(j = 5, fn = scales::label_percent()) |>
  format_tt(escape = TRUE)
date_lookup price price_rank memory speed_benchmark
2024-01-15 18.49 1st 16 GB 0.60
2024-01-18 19.99 2nd 12 GB 0.73
2024-01-14 24.99 3rd 10 GB 0.82
2024-01-16 24.99 3rd 8 GB 0.99

Created on 2024-02-21 with reprex v2.1.0

vincentarelbundock commented 9 months ago

Oof, yeah, that probably shouldn't be the behavior. I opened a separate issue for that.