I am running the following code in R:
library(modelsummary)
library(kableExtra)
tmp <- mtcars[, c("mpg", "hp")]
# create a list with individual variables
# remove missing and rescale
tmp_list <- lapply(tmp, na.omit)
tmp_list <- lapply(tmp_list, scale)
# create a table with `datasummary`
# add a histogram with column_spec and spec_hist
# add a boxplot with colun_spec and spec_box
emptycol = function(x) " "
datasummary(mpg + hp ~ Mean + SD + Heading("Boxplot") * emptycol + Heading("Histogram") * emptycol, data = tmp, output='latex') %>%
column_spec(column = 4, image = spec_boxplot(tmp_list)) %>%
column_spec(column = 5, image = spec_hist(tmp_list))
Source: https://vincentarelbundock.github.io/modelsummary/articles/datasummary.html#histograms-1
which gives me the following table in R:
By specifying output='latex', we obtain LaTeX code to generate the table in LaTeX:
\documentclass[a4,11pt]{article}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{siunitx}
\newcolumntype{d}{S[input-symbols = ()]}
\begin{document}
\begin{table}
\centering
\begin{tabular}[t]{lrr>{}r>{}r}
\toprule
& Mean & SD & Boxplot & Histogram\\
\midrule
mpg & \num{20.09} & \num{6.03} & \includegraphics[width=0.67in, height=0.17in]{} & \includegraphics[width=0.67in, height=0.17in]{}\\
hp & \num{146.69} & \num{68.56} & \includegraphics[width=0.67in, height=0.17in]{} & \includegraphics[width=0.67in, height=0.17in]{}\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
However, I get an error in TeX when running this code because there is information missing such as \includegraphics[width=0.67in, height=0.17in]{MISSING}
I think this might be because we are asking for the output before this piece of code:
%>%
column_spec(column = 4, image = spec_boxplot(tmp_list)) %>%
column_spec(column = 5, image = spec_hist(tmp_list))
Is there a way to include this piece of code in the main datasummary?
Edit:
When I try to export the table in pdf format via R Markdown as per the example code below:
---
title: "table1"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
```{r prep-tableone, message=FALSE, fig.pos="H"}
tmp <- mtcars[, c("mpg", "hp")]
# create a list with individual variables
# remove missing and rescale
tmp_list <- lapply(tmp, na.omit)
tmp_list <- lapply(tmp_list, scale)
# watch out that order of variables here must match exactly the one in the list above
emptycol = function(x) " "
table_output <- datasummary(mpg + hp ~ Min + Max + Mean + Heading("Boxplot") * emptycol + Heading("Histogram") * emptycol, data = tmp) %>%
column_spec(column = 5, image = spec_boxplot(tmp_list)) %>%
column_spec(column = 6, image = spec_hist(tmp_list))
```
```{r tableone}
table_output
```
After clicking on 'knit', I get the following error message:
Any ideas about what might be happening here?
I believe that this is a limitation in the kableExtra
package, and not in the modelsummary
package, as reported here. (Note that modelsummary
does not actually draw tables itself, but rather supports external packages to do it -- kableExtra
, gt
, flextable
, huxtable
).
Right now, kableExtra
tables with in-line histograms or boxplots only work out-of-the-box for HTML output or in PDF documents produced by Rmarkdown.
There might be a way to save the mini-plots in .SVG format for later use in a separate LaTeX document. My guess is that this is going to be quite tricky, but you may want to read the hints at the Github link posted above.
Here is a minimal example which reproduces your problem with a simple kableExtra
table (note again that modelsummary
is not involved):
library(kableExtra)
dat_hist = list(rnorm(100), rnorm(100))
data.frame(num = 1:2, plt = c("", "")) |>
kbl(format = "latex") |>
column_spec(column = 2, image = spec_hist(dat_hist)) |>
cat()
#
# \begin{tabular}[t]{r|>{}l}
# \hline
# num & plt\\
# \hline
# 1 & \includegraphics[width=0.67in, height=0.17in]{}\\
# \hline
# 2 & \includegraphics[width=0.67in, height=0.17in]{}\\
# \hline
# \end{tabular}
You may want to file a feature request on the kableExtra
github repository, using the minimal example.