Rmarkdown output to word creates one table instead of two

我的未来我决定 提交于 2020-01-30 06:19:50

问题


Rmarkdown output to word creates one table instead of two

The code below is expected to create two independent tables when I knit a Word document using Rmarkdown in Rstudio, but instead it creates one combined table.

Code expected to create a word document with two tables:

---
title: "Untitled"
output: word_document
---

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

## R Markdown


```{r}
knitr::kable(head(iris))
```


```{r}
knitr::kable(head(iris))
```

But it only creates one:

If, however, I set echo=TRUE, I get the expected two tables. When I output the same document to HTML it also creates the expected two tables. The tables are created by clicking the knit-button in Rstudio.

The question is: How do I get two tables, instead of one?

To clarify: the problem is not that two tables are created with little space between them. The problem is that one table is created. On the picture attached to this question, it indeed appears to be two tables (because there are two headers), but it is actually one table. But the answer by Axeman solves this. I still believe this is a bug though, because in earlier versions of knitr it was not necessary to specify <br><br> between tables, to ensure that they were not combined into one.

I filed a bug-report, but no one has seen it: https://support.rstudio.com/hc/en-us/community/posts/115008683128-Bug-Two-tables-produced-by-knitr-kable-are-rendered-as-one-in-word-output

Rstudio version 1.0.153

> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Danish_Denmark.1252  LC_CTYPE=Danish_Denmark.1252    LC_MONETARY=Danish_Denmark.1252 LC_NUMERIC=C                    LC_TIME=Danish_Denmark.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.4.1  backports_1.1.0 magrittr_1.5    rprojroot_1.2   htmltools_0.3.6 tools_3.4.1     yaml_2.1.14     Rcpp_0.12.12    rmarkdown_1.6   stringi_1.1.5  
[11] highr_0.6       knitr_1.16      digest_0.6.12   stringr_1.2.0   evaluate_0.10.1

On my machine at home I do not have this problem. So the same code produces independent tables, without needing to put <br><br> between the chunks. On this home machine the sessioninfo is this:

R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.4.1  backports_1.1.0 magrittr_1.5    rprojroot_1.2   htmltools_0.3.6 tools_3.4.1     yaml_2.1.14     Rcpp_0.12.12   
 [9] stringi_1.1.5   rmarkdown_1.6   knitr_1.17      stringr_1.2.0   digest_0.6.12   evaluate_0.10.1

回答1:


Put an HTML comment in between, and you get a more clear separation between your tables.

E.g.

---
title: "Untitled"
output: word_document
---

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

## R Markdown

```{r}
knitr::kable(head(iris))
```

<!-- -->

```{r}
knitr::kable(head(iris))
```

If this fails, you can put a newline instead, replace <!-- --> with <br><br>.



来源:https://stackoverflow.com/questions/45996337/rmarkdown-output-to-word-creates-one-table-instead-of-two

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!