Closed avehtari closed 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")
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
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}
Closing now to cleanup the repo, but feel free to keep the conversation going if the current dev version does not meet your needs.
Great!
I've found tinytable to be very nice, but encountered couple issue. Here's one
This works
but following does not
producing the following error
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.