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

markdown hyperlinks in `tt()` tables don't work as expected #239

Closed friendly closed 6 months ago

friendly commented 6 months ago

In my heplots package, I'm preparing several tables of datasets in the package, with the package name to be a link to the documentation in the pkgdown web site.

I get different results when I try tt() in the README vs. a datasets vignette, where the links don't resolve, so I had to revert to using knitr::kable(). There is probably tt() option or chunk option I'm missing, but I don't know what.

In the README.Rmd file, that becomes index.html I have this code:

library(here)
library(dplyr)
library(tinytable)
#dsets <- read.csv(here::here("extra", "datasets.csv"))
dsets <- read.csv("https://raw.githubusercontent.com/friendly/heplots/master/extra/datasets.csv")
dsets <- dsets |> dplyr::select(-X) |> arrange(tolower(dataset))

# link dataset to pkgdown doc
refurl <- "http://friendly.github.io/heplots/reference/"

dsets <- dsets |>
  mutate(dataset = glue::glue("[{dataset}]({refurl}{dataset}.html)")) 

tinytable::tt(dsets)
#knitr::kable(dsets)

This works, more or less as I expect, producing the Datasets table.

In the vignette, what I get when I knit this is:

> tinytable::tt(dsets)

+---------------------------------------------------------------------------------------+------+------+-------------------------------------------------------------------+------------------+
| dataset                                                                               | rows | cols | title                                                             | tags             |
+=======================================================================================+======+======+===================================================================+==================+
| [AddHealth](http://friendly.github.io/heplots/reference/AddHealth.html)               | 4344 |  3   | Adolescent Health Data                                            | MANOVA ordered   |
+---------------------------------------------------------------------------------------+------+------+-------------------------------------------------------------------+------------------+
| [Adopted](http://friendly.github.io/heplots/reference/Adopted.html)                   |   62 |  6   | Adopted Children                                                  | MMRA repeated    |
+---------------------------------------------------------------------------------------+------+------+-------------------------------------------------------------------+------------------+
| [Bees](http://friendly.github.io/heplots/reference/Bees.html)                         |  246 |  6   | Captive and maltreated bees                                       | MANOVA           |
+---------------------------------------------------------------------------------------+------+------+-------------------------------------------------------------------+------------------+
| [Diabetes](http://friendly.github.io/heplots/reference/Diabetes.html)                 |  145 |  6   | Diabetes Dataset                                                  | MANOVA           |
+---------------------------------------------------------------------------------------+------+------+-------------------------------------------------------------------+------------------+
| [FootHead](http://friendly.github.io/heplots/reference/FootHead.html)                 |   90 |  7   | Head measurements of football players                             | MANOVA contrasts |
+---------------------------------------------------------------------------------------+------+------+-------------------------------------------------------------------+------------------+
| [Headache](http://friendly.github.io/heplots/reference/Headache.html)                 |   98 |  6   | Treatment of Headache Sufferers for Sensitivity to Noise          | MANOVA repeated  |
+---------------------------------------------------------------------------------------+------+------+-------------------------------------------------------------------+------------------+
vincentarelbundock commented 6 months ago

pkgdown appears to process these two files differently:

When you call tt() in a Rmarkdown file that will output to HTML, tinytable will create an HTML table, but it doesn't know anything about what you put in that table. If you want content to be interpreted as markdown syntax to be converted to link with HTML tabs, you need to state that explicitly:

library(tinytable)
tt(x) |> format_tt(markdown = TRUE)

I tried this in your repo's vignette and it worked as expected. Don't hesitate to ping me if you try it and it doesn't work.

friendly commented 6 months ago

Wow! that is so helpful, and explains the problem. I'll give it a whirl.

friendly commented 6 months ago

Now works like a charm.

I wonder if what I've done is of any interest re your https://vincentarelbundock.github.io/Rdatasets/datasets.html which is the most comprehensive list of datasets in R packages I've seen.

vincentarelbundock commented 6 months ago

Great!

And thanks for the nudge. heplots has been available for a few months, but I ran the script again to update the files. Should live soon.