Closed dhicks closed 2 months ago
Ah, I see what you mean. That's a reasonable use-case, and it is not easy with the current CRAN version.
However, I just pushed a new feature to Github to allow i
to accept a logical matrix. To use it, install the development version:
remotes::install_github("vincentarelbundock/tinytable")
Restart R
completely for the change to take effect. Then,
library(tinytable)
cormat <- data.frame(cor(mtcars[1:5]))
tt(cormat, digits = 2) |>
style_tt(abs(cormat) > .8, color = "red")
This is a followup to this Bsky thread.
Here's a motivating example. I'm preparing a correlation table for a journal article. Because looking at a wall of correlations is godsawful, I want to put correlations in bold if their absolute value is greater than 0.8.
To set up, let's create a correlation table using
mtcars
. I'm going to coerce to a tibble just so I can convert rownames into a column in a single line.The suggestion on Bsky was to follow the conditional styling tutorial. Even setting aside how to exclude
variable
and account for absolute value, it's not clear how this is supposed to work, because I want to consider every column (exceptvariable
), not just certain particular columns. Something likewhich(dataf > .8)
returns the index of elements inunlist(dataf)
, a 1-dimensional vector.which
does have an argument that makes it return the coordinates ofTRUE
cells, in a sparse matrix format:But using this to pass columns and rows doesn't seem to work. Everything ends up in bold. I'm guessing the values passed to
i
andj
are handled (separately) as the unique rows and columns where styling should be applied? Rather than as paired row and column coordinates.I took a couple of minutes to trace down through the call to
style_tt
, trying to figure out howi
andj
are handled when they're both integer vectors. But I ran into the genericstyle_eval
and don't have time to dig further today.Self-contained MWE: