0

我在 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中的下表: 在此处输入图像描述

通过指定 output='latex',我们获得 LaTeX 代码以在 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}

但是,在运行此代码时,我在 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 格式导出表格时:

---
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”后,我收到以下错误消息: 在此处输入图像描述

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

4

1 回答 1

1

我相信这是kableExtra包装中的限制,而不是包装中的限制modelsummary,如此处报道的。(请注意,它本身并不实际绘制表格,modelsummary而是支持外部包来执行它—— kableExtra、、、、gt)。flextablehuxtable

目前,kableExtra带有直方图或箱线图的表格仅适用于 HTML 输出或 Rmarkdown 生成的 PDF 文档。可能有一种方法可以将迷你图保存为 .SVG 格式,以便以后在单独的 LaTeX 文档中使用。我的猜测是这会很棘手,但您可能想阅读上面发布的 Github 链接中的提示。

这是一个使用简单kableExtra表格重现您的问题的最小示例(再次注意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 存储库上提交功能请求。

于 2021-10-30T12:16:29.367 回答