vincentarelbundock / tinytable

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

Control column widths in table using HTML links #243

Closed friendly closed 4 months ago

friendly commented 4 months ago

Following #214 I'd like to produce a table controlling the widths of columns separately, but where the first column is an HTML link.

In the README for the HistData package, I tried:

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

dsets <- vcdExtra::datasets("HistData") |> 
  dplyr::select(Item, Title) |> 
  dplyr::mutate(Item = glue::glue("[{Item}]({refurl}{Item}.html)")) 

#knitr::kable(dsets)
library(tinytable)
tt(dsets) |>
  format_tt(j = 1, markdown = TRUE) |>
  style_tt(j = 1, bootstrap_css = "width: 30%;") |> 
  style_tt(j = 2, bootstrap_css = "width: 70%;")

But what I get has two columns of ~ equal width. Also tried tt(dsets, width=c(.3, .7)) but this gives an error saying width must be of length 1.

image

vincentarelbundock commented 4 months ago

Thanks for the report.

The code below should work when using the development version from Github:

remotes::install_github("vincentarelbundock/tinytable")

I hope to release a new version with these changes to CRAN in the next couple weeks.

library(tinytable)

dat <- data.frame
refurl <- "http://friendly.github.io/HistData/reference/"
dsets <- vcdExtra::datasets("HistData") |> 
  dplyr::select(Item, Title) |> 
  dplyr::mutate(Item = glue::glue("[{Item}]({refurl}{Item}.html)")) 

tt(dsets, width = c(.3, .7)) |> 
    format_tt(j = 1, markdown = TRUE)
friendly commented 4 months ago

I installed the latest tinytable, ver ‘0.2.1.14’ but I still get the same result, that the Item column takes up half the table width.

I also tried doing this as below, with the same result.

library(tinytable)
tt(dsets) |>
  format_tt(j = 1, markdown = TRUE) |>
  style_tt(j = 1, bootstrap_css = "width: 30%;") |>
  style_tt(j = 2, bootstrap_css = "width: 70%;")

Is there some way I can figure out why this is not working for me?

vincentarelbundock commented 4 months ago

Sorry, I'm travelling now, but I'll reopen the issue and circle back to it as soon as possible.

vincentarelbundock commented 4 months ago

For reference, here's what I get with with version 0.2.1.15 and this Quarto document. Full HTML here in case you want to compare to yours: https://gist.github.com/vincentarelbundock/7270c841e86507617049bc33c5a38c4f

---
format: html
---

```{r}
library(tinytable)

packageVersion("tinytable")

dat <- data.frame
refurl <- "http://friendly.github.io/HistData/reference/"
dsets <- vcdExtra::datasets("HistData") |> 
  dplyr::select(Item, Title) |> 
  dplyr::mutate(Item = glue::glue("[{Item}]({refurl}{Item}.html)")) 

tt(dsets, width = c(.3, .7)) |> format_tt(j = 1, markdown = TRUE)


![quarto](https://github.com/vincentarelbundock/tinytable/assets/987057/d7666d62-33f7-4378-80fd-b7dcef481375)
vincentarelbundock commented 4 months ago

Night be related to the output format, where Rmarkdown "smartly" overrides the settings. The output format and result of rmarkdown::pandoc_to() run in a knitted docs would be useful.

friendly commented 4 months ago

This is in a README.Rmd where I use

---
output: github_document
---

so knitr::pandoc_to() gives "gfm".

I used to use knitr::kable(dset) for this with similar results, but I hoped that using tinytable could allow me to control the width of the first column.

When I run this code in the console, the output in the Viewer panel looks like what I want (below), but I don't get that in what is generated for GitHub or pkgdown.

image

I'm mystified, but I guess this is not a tinytable problem.

vincentarelbundock commented 4 months ago

Aaaah! Well that explains it.

In that context, tinytable correctly infers that you are asking for a markdown document, so it returns a grid-style markdown table, not an HTML table. None of the HTML settings are retained in markdown tables, so the final display is determined by whatever CSS Github or pkgdown chooses to apply to your files.

One option you might want to explore is:

  1. Force tinytable to return an HTML table by using save_tt() instead of relying on automatic output inference.
  2. Call knitr::asis_output() to mark the output as "raw" HTML.

I believe --- but am not 100% sure --- that Github and pkgdown can interpret raw HTML blocks in markdown documents. I'd be curious to know if something like this works:

---
output: github_document
---

# This is a title

blah blah blah

```{r}
library(tinytable)
x <- mtcars[1:4, 1:4]
tt(x) |> 
    style_tt(1:2, background = "teal", color = "white") |>
    save_tt("html") |>
    knitr::asis_output()
vincentarelbundock commented 4 months ago

OK, I've spent a ton of time on this and I give up. This is not working because pkgdown is doing something weird, and I can't figure out what "magic" behavior is breaking things. In any case, I don't think there's much I can change in tinytable to fix this.

For what it's worth, this works effortlessly using the development version of altdoc. Here's a live example of your heplots website, rendered by Quarto:

https://arelbundock.com/heplots

First, convert the home page to Quarto:

mv README.Rmd README.qmd

Then, substitute all instances of link-citations: yes for link-citations: true

Intall the dev versions of tinytable and altdoc:

remotes::install_github("etiennebacher/altdoc")
remotes::install_github("vincentarelbundock/tinytable")

Setup and render the website:

altdoc::setup_docs("quarto_website")
altdoc::render_docs(verbose = TRUE)
friendly commented 4 months ago

Thank you so much for following up on this; I can appreciate the effort you took to try to track this down. I'll consider switching to altdoc, though all my other pkgs use pandoc.