Skip to content

Commit e096c57

Browse files
committed
Add tinytable examples, and improve package documentation section
1 parent e736cc0 commit e096c57

File tree

1 file changed

+79
-7
lines changed

1 file changed

+79
-7
lines changed

vignettes/markdown-html-tables.qmd

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,26 @@ Using `data-qmd` or `data-qmd-base64` attributes is a Quarto-specific feature an
133133

134134
## Table package integration
135135

136+
To summarize the question of Markdown processing in HTML tables within Quarto,
137+
138+
- This is possible thanks to Quarto HTML Table parsing
139+
- This is done using some `<span>` or `<div>` elements with `data-qmd` or `data-qmd-base64` attributes
140+
141+
Any R package for producing tables and providing raw HTML as output needs to support this Quarto feature to allow Markdown content in HTML tables.
142+
143+
Currently, there is two ways this could be supported:
144+
145+
- Either the package already supports Quarto HTML table parsing, and offer a way to mark the cells as to be processed by Quarto when in Quarto context. In this case, they will create themself the `<span>` or `<div>` elements with the `data-qmd` or `data-qmd-base64` attributes.
146+
147+
- Either the package does not support Quarto HTML table parsing directly, and offers a way to insert raw HTML content in the table cells. In this case, you can use the helper functions provided by this package to create the `<span>` or `<div>` elements with the appropriate attributes.
148+
149+
Below will show how this works with some popular R packages for creating tables.
150+
136151
### Using with kableExtra
137152

138-
Here's a more complex example that combines all these features to create a complete HTML table with Markdown content:
153+
**kableExtra** is a popular package for creating and styling tables in R. It produces raw HTML but does not have yet specific support for Quarto's HTML table parsing. However, you can use the helper functions to insert Markdown content into the cells, as it allows to insert raw HTML content in the table cells (by setting `escape = FALSE` to keep the raw HTML as-is).
154+
155+
So here is a more complex example that combines all these features to create a complete HTML table with Markdown content:
139156

140157
```{r}
141158
library(quarto)
@@ -168,25 +185,29 @@ kbl(complex_table, format = "html", escape = FALSE) %>%
168185
row_spec(0, bold = TRUE, background = "#f8f8f8")
169186
```
170187

188+
### Using with **flextable**
171189

172-
### Using with flextable
190+
By design, **flextable** does not support inserting raw HTML content into its cells. Using the `tbl_qmd_span()` or `tbl_qmd_div()` functions directly in a flextable will not work as expected.
173191

174-
By design, flextable does not support inserting raw HTML content into its cells. Using the `tbl_qmd_span()` or `tbl_qmd_div()` functions directly in a flextable will not work as expected.
192+
Unfortunately, **flextable** does not yet integrate with Quarto's HTML table parsing features, and does not allow to mark cell content as Markdown to be processed by Quarto.
175193

176-
Quarto team will be working with flextable developers to find a way to support this in the future.
194+
The Quarto team will be working with **flextable** developers to find a way to support this in the future.
177195

178196
### Using with **gt**
179197

180-
The **gt** package provides a way to create tables with rich formatting. **gt** already has built-in support for rendering Markdown content, so you can use it directly without needing the `tbl_qmd_span()` or `tbl_qmd_div()` functions.
198+
The **gt** package provides a way to create tables with rich formatting.
199+
200+
**gt** allows to insert raw HTML content in the table cells, and it has built-in support for Quarto's HTML table parsing. It uses the `data-qmd` attribute internally to mark cells that contain Markdown content.
181201

182-
Here is the same example as above, but using **gt** with **quarto** R package functions
202+
Here is the same table example as above, using **gt** with **quarto** R package functions. `fmt_passthrough()` is used to allow raw HTML content in the table cells, and `escape = FALSE` is set to avoid escaping the HTML content:
183203

184204
```{r}
185205
library(gt)
186206
gt(complex_table) |>
187207
fmt_passthrough(columns = "Example", escape = FALSE)
188208
```
189209

210+
However, **gt** already has built-in support for rendering Markdown content, so you can use it directly without needing the `tbl_qmd_span()` or `tbl_qmd_div()` functions.
190211
Here is the example with built-in support for Markdown content in **gt**:
191212

192213
```{r}
@@ -212,8 +233,59 @@ data.frame(
212233

213234
`gt::fmt_markdown()` is aware of Quarto context and it will internally use the `data-qmd` attribute to render Markdown content correctly when Quarto processes the document.
214235

236+
### Using with **tinytable**
237+
238+
From the **tinytable** package website (<https://vincentarelbundock.github.io/tinytable/>):
239+
> `tinytable` is a small but powerful R package to draw beautiful tables in a variety of formats: HTML, LaTeX, Word1, PDF, PNG, Markdown, and Typst.
240+
241+
By default, `tinytable` does deactivate Quarto HTML table processing. It is a design choice so that **tinytable** formatting is not affected by Quarto's HTML table processing. So, our previous table would look like this:
242+
243+
```{r}
244+
library(tinytable)
245+
246+
tt(complex_table)
247+
```
248+
249+
Note that display value for video shortcode is used, as the shortcode is not processed by Quarto in this case.
250+
251+
Quarto HTML table processing can be re-enabled in **tinytable**, and in that case, they will handle the `data-qmd` attribute internally, and functions `tbl_qmd_span()` and `tbl_qmd_div()` will not be needed.
252+
253+
```{r}
254+
options(tinytable_quarto_disable_processing = FALSE)
255+
tt(complex_table)
256+
```
257+
258+
Setting the option will opt-in the Quarto HTML table processing for all tables created with **tinytable**. This allows a table using `tbl_qmd_span()` or `tbl_qmd_div()` to be processed correctly by Quarto.
259+
Let's unset option
260+
261+
```{r}
262+
options(tinytable_quarto_disable_processing = NULL)
263+
```
264+
265+
Note that **tinytable** does support `data-qmd` attributes internally, so functions `tbl_qmd_span()` and `tbl_qmd_div()` are not needed when using **tinytable**. You can use `tt()` function directly with Markdown content in the table cells, and marking the cells as using Quarto Markdown processing.
266+
267+
```{r}
268+
data.frame(
269+
Feature = c("Formatting", "Math", "References", "Media"),
270+
Example = c(
271+
c("**Bold**, *italic*, and `code`"),
272+
"$\\int_{a}^{b} f(x) \\, dx$",
273+
"See @tbl-kable-equation for example of a table",
274+
"{{< video https://www.youtube.com/embed/wo9vZccmqwc >}}"
275+
),
276+
Notes = c(
277+
"Basic markdown formatting",
278+
"LaTeX math expressions",
279+
"Cross-references to other document elements",
280+
"Embedded media using shortcodes"
281+
)
282+
) |>
283+
tt() |>
284+
format_tt(j = "Example", quarto = TRUE)
285+
```
286+
215287
## Conclusion
216288

217-
The table helper functions in this package make it easy to include Markdown content in HTML tables when working with Quarto documents. They will be useful to users to get unblocked when using package that provide HTML tables. Hopefully, developers will also find them useful to simplify the process for users of creating tables with rich content.
289+
The table helper functions in this package make it easy to include Markdown content in HTML tables when working with Quarto documents. They will be useful to users to get unblocked when using a package that provide HTML tables, and does already support Quarto processing. Hopefully, developers will also find them useful to simplify the process for users of creating tables with rich content. This is already happening with **gt** and **tinytable** packages, which have built-in support for Markdown content in tables by marking the cells with the `data-qmd` attribute, internally for the user.
218290

219291
For more information about tables in Quarto, see the [Quarto documentation on tables](https://quarto.org/docs/authoring/tables.html#html-tables).

0 commit comments

Comments
 (0)