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

Support tibble numeric formatting #141

Closed avehtari closed 9 months ago

avehtari commented 9 months ago

I've found tinytable to be very nice, but encountered couple issue. Here's one

This works

tibble(
  x3 = 9:11 * 100 + 0,
  x4 = 9:11 * 100 + 0.5,
  x5 = 9:11 * 100 + 0.5,
) |> tt()

but following does not

tibble(
  x3 = num(9:11 * 100 + 0.5, sigfig = 3),
  x4 = num(9:11 * 100 + 0.5, sigfig = 4),
  x5 = num(9:11 * 100 + 0.5, sigfig = 5),
) |> tt()

producing the following error

+ Error in `FUN()`:
! Can't convert `x` <pillar_num:3> to <character>.
Run `]8;;rstudio:run:rlang::last_trace()rlang::last_trace()]8;;` to see where the error occurred.

It would be cool if tinytable would support directly the num formatting (e.g. sigfig here), but if that is too much to ask, it would be nice to not get that error.

vincentarelbundock commented 9 months ago

Thanks for the feature request. This should be fixed by https://github.com/vincentarelbundock/tinytable/commit/6bb2c75023a163aa5bf201398fffe821240abbb5, now on Github main.

Frankly, I'm not an expert in the handling of ANSI strings like the ones that tibble and pillar generate, but I think that as long as the user has the fansi package installed, the current dev version should convert ANSI control sequences to HTML or strip them depending on the output format. fansi is a dependency of tibble, so it should be installed on computers where this is relevant.

library(tibble)
library(tinytable)

k = tibble(
  x3 = num(9:11 * 100 + 0.5, sigfig = 3),
  x4 = num(9:11 * 100 + 0.5, sigfig = 4),
  x5 = num(9:11 * 100 + 0.5, sigfig = 5),
) 

tt(k) |> print("markdown")
# 
# 
# 
# +-------+--------+--------+
# | x3    | x4     | x5     |
# +=======+========+========+
# |  900. |  900.5 |  900.5 |
# +-------+--------+--------+
# | 1000. | 1000.  | 1000.5 |
# +-------+--------+--------+
# | 1100. | 1100.  | 1100.5 |
# +-------+--------+--------+

tt(k) |> print("latex")
# 
# \begin{table}
# \centering
# \begin{tblr}[         %% tabularray outer open
# ]                     %% tabularray outer close
# {                     %% tabularray inner open
# colspec={Q[]Q[]Q[]},
# }                     %% tabularray inner close
# \toprule
# x3 & x4 & x5 \\ \midrule %% TinyTableHeader
# 900. &  900.5 &  900.5 \\
# 1000. & 1000.  & 1000.5 \\
# 1100. & 1100.  & 1100.5 \\
# \bottomrule
# \end{tblr}
# \end{table}

tt(k) |> print("html")

html

avehtari commented 9 months ago

That commit breaks the behavior for a tibble without num

library(tibble)
library(tinytable)

k2 = tibble(
  x3 = 9:11 * 100 + 0.1,
  x4 = 9:11 * 100 + 0.12,
  x5 = 9:11 * 100 + 0.123,
) 

tt(k2, digits=1) |> print("markdown")

gives with any formatting options

+--------------------------+-----------------------------+--------------------------------+
| x3                       | x4                          | x5                             |
+==========================+=============================+================================+
| c(900.1, 1000.1, 1100.1) | c(900.12, 1000.12, 1100.12) | c(900.123, 1000.123, 1100.123) |
+--------------------------+-----------------------------+--------------------------------+
| c(900.1, 1000.1, 1100.1) | c(900.12, 1000.12, 1100.12) | c(900.123, 1000.123, 1100.123) |
+--------------------------+-----------------------------+--------------------------------+
| c(900.1, 1000.1, 1100.1) | c(900.12, 1000.12, 1100.12) | c(900.123, 1000.123, 1100.123) |
+--------------------------+-----------------------------+--------------------------------+

EDIT: first copied the wrong table

vincentarelbundock commented 9 months ago

Oof, that’s embarassing. I added your example to the test suite and pushed a fix.

library(tibble)
library(tinytable)

k2 = tibble(
  x3 = 9:11 * 100 + 0.1,
  x4 = 9:11 * 100 + 0.12,
  x5 = 9:11 * 100 + 0.123,
) 

tt(k2, digits=1) |> print("markdown")
# 
# 
# 
# +------+------+------+
# | x3   | x4   | x5   |
# +======+======+======+
# |  900 |  900 |  900 |
# +------+------+------+
# | 1000 | 1000 | 1000 |
# +------+------+------+
# | 1100 | 1100 | 1100 |
# +------+------+------+

tt(k2, digits=1) |> print("latex")
# 
# \begin{table}
# \centering
# \begin{tblr}[         %% tabularray outer open
# ]                     %% tabularray outer close
# {                     %% tabularray inner open
# colspec={Q[]Q[]Q[]},
# }                     %% tabularray inner close
# \toprule
# x3 & x4 & x5 \\ \midrule %% TinyTableHeader
# 900 &  900 &  900 \\
# 1000 & 1000 & 1000 \\
# 1100 & 1100 & 1100 \\
# \bottomrule
# \end{tblr}
# \end{table}
vincentarelbundock commented 9 months ago

Closing now to cleanup the repo, but feel free to keep the conversation going if the current dev version does not meet your needs.

avehtari commented 9 months ago

Great!