【问题标题】:how to include 'column_spec' in the output of datasummary?如何在 datasummary 的输出中包含“column_spec”?
【发布时间】:2021-10-29 19:44:19
【问题描述】:

我在 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”后,我收到以下错误消息:

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

【问题讨论】:

    标签: r kable kableextra modelsummary


    【解决方案1】:

    我认为这是kableExtra 包中的限制,不是modelsummary 包中的限制,如reported here.(请注意modelsummary 本身实际上并不绘制表格,而是支持外部包来做到这一点——kableExtragtflextablehuxtable)。

    目前,带有直方图或箱线图的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 存储库上提交功能请求。

    【讨论】:

    • 谢谢文森特!这是有道理的,我刚刚在 GitHub 上提出了一个关于此的问题。同时,我正在听从您的建议,我正在尝试通过 R Markdown 导出带有箱形图和 hist 的表格。但是,当我点击“编织”时,我得到了上面的错误。我已经编辑了我的问题以包含这种情况 - 请在“编辑”之后查看。你能发现我的错误吗?非常感谢!
    • %&gt;% 函数不是 Base R 函数。您需要加载提供此功能的库,例如tidyversemagrittrkableExtra。当然,您还需要加载 modelsummary 包。
    • 我以为包已经通过控制台加载了..我的错!感谢您指出。有效!现在,我终于可以尝试使用因子和数值变量创建自己的表了。我刚刚发布了一个关于这个问题的问题,如果你有时间帮忙的话!提前致谢
    猜你喜欢
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-24
    相关资源
    最近更新 更多