如何在 datasummary 的输出中包含 'column_spec'?

how to include 'column_spec' in the output of datasummary?

我是 运行 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))

来源: https://vincentarelbundock.github.io/modelsummary/articles/datasummary.html#histograms-1

这在 R 中给出了以下 table:

通过指定output='latex',我们得到LaTeX代码生成LaTeX中的table:

\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}

但是,当 运行 这段代码时,我在 TeX 中遇到错误,因为缺少 \includegraphics[width=0.67in, height=0.17in]{MISSING}

等信息

我认为这可能是因为我们在这段代码之前要求输出:

  %>%
  column_spec(column = 4, image = spec_boxplot(tmp_list)) %>%
  column_spec(column = 5, image = spec_hist(tmp_list))

有没有办法将这段代码包含在主数据摘要中?

编辑:

当我尝试按照以下示例代码通过 R Markdown 以 pdf 格式导出 table 时:

---
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

```

点击 'knit' 后,我收到以下错误消息:

对这里可能发生的事情有什么想法吗?

我认为这是 kableExtra 包中的限制,而 不是 modelsummary 包中的限制,因为 reported here. (注意modelsummary 实际上并不绘制 tables 本身,而是支持外部包来完成它 -- kableExtragtflextablehuxtable).

目前,kableExtra table 内联直方图或箱线图仅适用于 HTML 输出或 Rmarkdown 生成的 PDF 文档。 可能 有一种方法可以将小图保存为 .SVG 格式,以便以后在单独的 LaTeX 文档中使用。我的猜测是这将非常棘手,但您可能需要阅读上面发布的 Github link 中的提示。

这是一个最小的例子,它用简单的 kableExtra table 重现了你的问题(再次注意 modelsummary 不涉及):

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}

您可能希望使用最小示例在 kableExtra github 存储库上提交功能请求。