Rmarkdown输出到word将创建一个表而不是两个表 [英] Rmarkdown output to word creates one table instead of two

查看:356
本文介绍了Rmarkdown输出到word将创建一个表而不是两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rmarkdown输出到word将创建一个表而不是两个表

当我在Rstudio中使用Rmarkdown编织Word文档时,预期下面的代码将创建两个独立的表,但是它将创建一个组合表.

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.

期望用两个表创建一个word文档的代码:

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

但是它只会创建一个:

但是,如果我将echo设置为TRUE,则会得到预期的两个表.当我将同一文档输出到HTML时,它还会创建预期的两个表. 通过单击Rstudio中的编织按钮来创建表.

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?

要澄清:问题不是两个表之间创建的空间很小.问题是创建了一个表.在与此问题相关的图片上,它的确确实是两个表(因为有两个标题),但实际上是一个一个表.但是阿克斯曼的答案解决了这个问题.我仍然相信这是一个错误,因为在较早版本的knitr中,不必在表之间指定<br><br>,以确保它们不会合并为一个.

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

在家里的机器上,我没有这个问题.因此,相同的代码将生成独立的表,而无需在块之间放置<br><br>.在此家用计算机上,sessioninfo是这样的:

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

推荐答案

在它们之间放置一个HTML注释,可以使表之间的分隔更加清晰.

Put an HTML comment in between, and you get a more clear separation between your 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))
```

如果失败,则可以换行,将<!-- -->替换为<br><br>.

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

这篇关于Rmarkdown输出到word将创建一个表而不是两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆