I've been trying to create tables using the gtsummary and gt packages in R.
When knitting to pdf in R Markdown, the tab spanner is too long for the table. This is not a problem when knitting to html. Not sure what's going on or how to fix it!
Reproducible example:
library(tidyverse)
library(gt)
library(gtsummary)
test <- function(data, variable, by,...) {
a = c(1,2,3)
names(a) = c("A", "B", "C")
as_tibble_row(a)
}
age = rnorm(100)
group = rbinom(100, 1, 0.5)
data= as.data.frame(cbind(age, group))
#Creates table
data %>%
tbl_summary(by=group) %>%
add_overall(last=T) %>%
add_stat(fns = list(everything()~test), location = everything() ~ "label") %>%
as_gt() %>%
gt::cols_width("label" ~ pct(25), stat_0 ~ pct(55/3), stat_1 ~ pct(55/3), stat_2 ~ pct(55/3), "A" ~ pct(20/3), "B" ~ pct(20/3), "C" ~ pct(20/3)) %>%
tab_spanner(label = "p-values",columns = c("A", "B", "C"))
This seems to be a bug, inspecting the LaTeX
\begin{table}[t] \fontsize{12.0pt}{14.4pt}\selectfont \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}>{\raggedright\arraybackslash}p{\dimexpr 0.25\linewidth -2\tabcolsep-1.5\arrayrulewidth}>{\centering\arraybackslash}p{\dimexpr 0.18\linewidth -2\tabcolsep-1.5\arrayrulewidth}>{\centering\arraybackslash}p{\dimexpr 0.18\linewidth -2\tabcolsep-1.5\arrayrulewidth}>{\centering\arraybackslash}p{\dimexpr 0.18\linewidth -2\tabcolsep-1.5\arrayrulewidth}>{\centering\arraybackslash}p{\dimexpr 0.07\linewidth -2\tabcolsep-1.5\arrayrulewidth}>{\centering\arraybackslash}p{\dimexpr 0.07\linewidth -2\tabcolsep-1.5\arrayrulewidth}>{\centering\arraybackslash}p{\dimexpr 0.07\linewidth -2\tabcolsep-1.5\arrayrulewidth}} \toprule ** HERE ** & & & & \multicolumn{3}{>{\centering\arraybackslash}m{\dimexpr 0.62\linewidth -2\tabcolsep-1.5\arrayrulewidth}}{p-values} \\ \cmidrule(lr){5-7} \textbf{Characteristic} & \textbf{0}\\ N = 49\textsuperscript{\textit{1}} & \textbf{1}\\ N = 51\textsuperscript{\textit{1}} & \textbf{Overall}\\ N = 100\textsuperscript{\textit{1}} & A & B & C \\ \midrule\addlinespace[2.5pt] age & 0.03 (-0.45, 0.75) & 0.21 (-0.37, 0.63) & 0.14 (-0.40, 0.67) & 1.00 & 2.00 & 3.00 \\ \bottomrule \end{tabular*} \begin{minipage}{\linewidth} \textsuperscript{\textit{1}}Median (Q1, Q3)\\ \end{minipage} \end{table}
with my limitted skills I can still see that the multicolumn
spans 0.62\linewidth which is 62 % of the writable page, which is why it looks so weird! You can fix this, but it's messy and hacky by replacing dimexpr 0.62
with dimexpr 0.21
using gsub
. It should be best, to open a bug report about this issue.
---
title: "Untitled"
output: pdf_document
date: "2025-05-16"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r cars, warning=FALSE,echo=FALSE}
library(tidyverse)
library(gt)
library(gtsummary)
test <- function(data, variable, by,...) {
a = c(1,2,3)
names(a) = c("A", "B", "C")
as_tibble_row(a)
}
age = rnorm(100)
group = rbinom(100, 1, 0.5)
data= as.data.frame(cbind(age, group))
#Creates table
t <- data %>%
tbl_summary(by=group) %>%
add_overall(last=T) %>%
add_stat(fns = list(everything()~test), location = everything() ~ "label") %>%
as_gt() %>%
gt::cols_width("label" ~ pct(25), stat_0 ~ pct(55/3), stat_1 ~ pct(55/3), stat_2 ~ pct(55/3), "A" ~ pct(20/3), "B" ~ pct(20/3), "C" ~ pct(20/3)) %>%
tab_spanner(label = "p-values",columns = c("A", "B", "C")) %>%
gt::as_latex()
t <- gsub("dimexpr 0.62","dimexpr 0.21",t)
```
```{r, results='asis'}
t
```
giving