I know there are issues with using loops and output of gtsummary tables in quarto. I’ve tried using
knitr::knit_print(tbl)
or cat(knitr::knit_print(tbl))
and can’t get that to work either.
I’m basically trying to make several tables looping over different grouping variables. I’ve made a reproducble example using the titanic dataset below (here trying to make tables splitting by “Survived” and “Sex”.
Any help on getting the tables to render in my html quarto output?
If i just run the chunk in rstudio, the tables are fine in my console.
<code>---
title: "titanic data - example"
author: "Me!"
date: "`r Sys.Date()`"
format:
html:
theme: cerulean
font-size: smaller
toc: true
toc-depth: 5
code-fold: true
code-tools: true
code-summary: "Show the code"
#code-copy: true
comments:
hypothesis: true
editor: visual
---
</code>
<code>---
title: "titanic data - example"
author: "Me!"
date: "`r Sys.Date()`"
format:
html:
theme: cerulean
font-size: smaller
toc: true
toc-depth: 5
code-fold: true
code-tools: true
code-summary: "Show the code"
#code-copy: true
comments:
hypothesis: true
editor: visual
---
</code>
---
title: "titanic data - example"
author: "Me!"
date: "`r Sys.Date()`"
format:
html:
theme: cerulean
font-size: smaller
toc: true
toc-depth: 5
code-fold: true
code-tools: true
code-summary: "Show the code"
#code-copy: true
comments:
hypothesis: true
editor: visual
---
<code># knitr::opts_chunk$set(fig.width = 6, fig.height = 4) #default size of figs in the doc
knitr::opts_chunk$set(warning = FALSE, message = FALSE) # default, suppress warnings & messages
</code>
<code># knitr::opts_chunk$set(fig.width = 6, fig.height = 4) #default size of figs in the doc
knitr::opts_chunk$set(warning = FALSE, message = FALSE) # default, suppress warnings & messages
</code>
# knitr::opts_chunk$set(fig.width = 6, fig.height = 4) #default size of figs in the doc
knitr::opts_chunk$set(warning = FALSE, message = FALSE) # default, suppress warnings & messages
<code>library(titanic)
library(gtsummary)
library(gt)
library(plotrix) # added b/c it has a std.error function
library(tidyverse)
variables <- c("Survived", "Sex")
for (var in variables) {
tab1 <- titanic_train %>%
select(!!sym(var), Age, Fare) %>%
gtsummary::tbl_summary(
by = !!sym(var),
missing = "ifany", # adds a row with number of missing, if any are missing
statistic = all_continuous() ~ "{mean} ({std.error})",
digits = all_continuous() ~ 1
) %>%
modify_header(all_stat_cols(FALSE) ~ ("**{level}**<br>N = {n}")) %>%
bold_labels() %>%
modify_spanning_header(all_stat_cols() ~ glue::glue("**Titanic split by - {var}**")) %>%
add_p(
test = everything() ~ "oneway.test",
test.args = all_tests("aov") ~ list(var.equal = TRUE)
) %>% gtsummary::as_gt()
print(tab1)
}
</code>
<code>library(titanic)
library(gtsummary)
library(gt)
library(plotrix) # added b/c it has a std.error function
library(tidyverse)
variables <- c("Survived", "Sex")
for (var in variables) {
tab1 <- titanic_train %>%
select(!!sym(var), Age, Fare) %>%
gtsummary::tbl_summary(
by = !!sym(var),
missing = "ifany", # adds a row with number of missing, if any are missing
statistic = all_continuous() ~ "{mean} ({std.error})",
digits = all_continuous() ~ 1
) %>%
modify_header(all_stat_cols(FALSE) ~ ("**{level}**<br>N = {n}")) %>%
bold_labels() %>%
modify_spanning_header(all_stat_cols() ~ glue::glue("**Titanic split by - {var}**")) %>%
add_p(
test = everything() ~ "oneway.test",
test.args = all_tests("aov") ~ list(var.equal = TRUE)
) %>% gtsummary::as_gt()
print(tab1)
}
</code>
library(titanic)
library(gtsummary)
library(gt)
library(plotrix) # added b/c it has a std.error function
library(tidyverse)
variables <- c("Survived", "Sex")
for (var in variables) {
tab1 <- titanic_train %>%
select(!!sym(var), Age, Fare) %>%
gtsummary::tbl_summary(
by = !!sym(var),
missing = "ifany", # adds a row with number of missing, if any are missing
statistic = all_continuous() ~ "{mean} ({std.error})",
digits = all_continuous() ~ 1
) %>%
modify_header(all_stat_cols(FALSE) ~ ("**{level}**<br>N = {n}")) %>%
bold_labels() %>%
modify_spanning_header(all_stat_cols() ~ glue::glue("**Titanic split by - {var}**")) %>%
add_p(
test = everything() ~ "oneway.test",
test.args = all_tests("aov") ~ list(var.equal = TRUE)
) %>% gtsummary::as_gt()
print(tab1)
}