【问题标题】:Why aren't tables from compareGroups displaying properly in Rmarkdown PDF?为什么 compareGroups 中的表格不能在 Rmarkdown PDF 中正确显示?
【发布时间】:2020-11-18 00:46:57
【问题描述】:

我有以下代码:

---
title: "test"
author: "author"
date: "2020/11/17"
output: pdf_document
always_allow_html: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r cars}
library(compareGroups)
export2md(createTable(compareGroups(Species~.,data=iris)))
```

我使用以下命令来运行这段代码:

rmarkdown::render("test.Rmd")

我希望表格看起来像这样:


Summary descriptives table by groups of `Species'
setosa  versicolor  virginica   p.overall
N=50    N=50    N=50    
Sepal.Length    5.01 (0.35) 5.94 (0.52) 6.59 (0.64) <0.001
Sepal.Width 3.43 (0.38) 2.77 (0.31) 2.97 (0.32) <0.001
Petal.Length    1.46 (0.17) 4.26 (0.47) 5.55 (0.55) <0.001
Petal.Width 0.25 (0.11) 1.33 (0.20) 2.03 (0.27) <0.001

但在我从 Rmarkdown 代码生成的 PDF 中最终看起来像这样:

library(compareGroups)
export2md(createTable(compareGroups(Species~.,data=iris)))
## Warning in export2md(createTable(compareGroups(Species ~ ., data = iris))): You are calling export2md ## -> html format is assigned
Summary descriptives table by groups of ‘Species’
setosa
versicolor
virginica
p.overall
N=50
N=50
N=50
Sepal.Length
5.01 (0.35)
5.94 (0.52)
6.59 (0.64)
<0.001
Sepal.Width
3.43 (0.38)
2.77 (0.31)
2.97 (0.32)
<0.001
Petal.Length
1.46 (0.17)
4.26 (0.47)
5.55 (0.55)
<0.001
Petal.Width
0.25 (0.11)

为什么会这样?如何才能在导出的 pdf 中正确显示表格?

【问题讨论】:

    标签: r pdf r-markdown comparegroups


    【解决方案1】:

    这样可以营造清新干净的环境。 resource info

    解决方案:

    xfun::Rscript_call(
    rmarkdown::render,
    list(input = 'test.Rmd', output_format = 'pdf_document'))
    

    其他可能的解决方案:

    不知道为什么,但在不更改代码的情况下,render() 确实产生了不正确的输出。但是使用 RStudio 中的 knit 按钮将其呈现到正确的输出

    但另一种方法是将其输出为 HTML,然后通过网络浏览器将其打印为 pdf 以用于另一个 PDF 文档输出,这是在处理表格和输出格式时获得一些最佳结果的官方方式。

    ---
    title: "test"
    author: "author"
    date: "2020/11/17"
    output: html_document
    always_allow_html: true
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ```{r cars}
    library(compareGroups)
    export2md(createTable(compareGroups(Species~.,data=iris)))
    ```
    

    【讨论】:

      【解决方案2】:

      问题已经解决(相当简单)。

      代码应该是:

      export2md(table_1, format="markdown")

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-13
        • 1970-01-01
        • 2012-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多