Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering Quarto document with multiple gt tables printed via purrr::map

Tags:

r

purrr

gt

quarto

I’m encountering an issue with rendering tables in my Quarto Markdown document. I’m using walk and map to generate tables with a custom function, but instead of displaying the tables, only the HTML code of the tables is printed.

Here is a reproducible example of my code:

---
title: "Table Rendering Issue"
output: html_document
---


```{r}
library(gt)
library(dplyr)
library(purrr)
# Creating a sample dataset
ds_sep <- data.frame(
  column_name = c("02", "02", "01"),
  var1 = c("A", "B", "C"),
  var2 = c(1, 2, 3)
)

# List of variables of interest
var_sociodemo <- c("var1", "var2")

# Custom function to create tables
table_sociodemo <- function(data, column, value) {
  data %>%
    filter(!!sym(column) == value) %>%
    gt()
}

# Generating and printing the tables
walk(map(var_sociodemo, ~table_sociodemo(ds_sep, "column_name", .x)), print)
```

I expect the tables to be displayed correctly in the document. Instead of the tables, I get only the HTML code, as shown below: enter image description here

How can I ensure that the tables are rendered correctly in the Quarto Markdown document instead of just getting the HTML code? Any suggestions or solutions would be greatly appreciated!

Thank you in advance!

like image 754
Erik De Luca Avatar asked Feb 01 '26 07:02

Erik De Luca


1 Answers

Borrowing as_raw_html() from Zé Loff's answer and htmltools::tagList() from this other SO answer, here's an adapted version of the OP's reprex that worked for me (sorry for the formatting, SO kept complaining that I wasn't formatting my code properly, perhaps because of the triple backticks):

---
title: "Table Rendering Issue"
format:
  pdf:
    documentclass: scrartcl
    papersize: letter
---

Then paste this in an r chunk with #|output: asis Quarto chunk option:

library(gt)
library(dplyr)
library(purrr)
# Creating a sample dataset
ds_sep <- data.frame(
  group = c("02", "02", "01"),
  var1 = c("A", "B", "C"),
  var2 = c(1, 2, 3)
)

# List of variables of interest
group_values <- c("01", "02")

# Custom function to create tables
table_sociodemo <- function(data, column, value) {
  data %>%
    filter(!!sym(column) == value) %>%
    gt() %>% 
    as_raw_html()
}

# Generating and printing the tables
htmltools::tagList(map(group_values, ~table_sociodemo(ds_sep, "group", .x)))
like image 84
Derek Burk Avatar answered Feb 03 '26 21:02

Derek Burk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!