R Markdown:防止代码结果中的分页符

Geo*_*ery 6 r r-markdown

我有一个Rmarkdown文档,我想编写成PDF文档.如何防止代码块结果中的分页符.

结果不超过一页.如果将一个太长的页面剩余结果移动到下一页而不是在第1页上显示表头而在第2页上显示表的其余部分,那将是明智的.

对不起,如果答案是在某处.我没找到它.

编辑:请求了一些代码.所以,我们走了.

---
title: "How to prevent page breaks in R Markdown code results"
author: "Georgery"
date: "10 January 2017"
output: pdf_document
---

# Create
# some
# headlines
# to
# fill
# the
# page
# a
# little
# and
# even
# a 
# little
# more
...and now create some code results
```{r, echo = FALSE}
data.frame(
    a = 1:20
    ,b = letters[1:20]
)
```
Run Code Online (Sandbox Code Playgroud)

Geo*_*ery 1

经过相当长的一段时间后回到这个问题并提出解决方案。

kable()对我来说最有效的就是从包装中使用knitr。这样,表格就会呈现为一个对象。这是代码:

---
title: "How to prevent page breaks in R Markdown code results"
author: "Georgery"
date: "10 January 2017"
output: pdf_document
---

# Create
# some
# headlines
# to
# fill
# the
# page
# a
# little
# and
# even
# a 
# little
# more
...and now create some code results

```{r, echo = FALSE, warning = FALSE, message = FALSE}
library(knitr) # needed to make the table a separate object on only one page
library(kableExtra) # not needed but makes the table nicer
library(tidyverse) # not needed at all, but I like the pipe (%>%)

data.frame(
    a = 1:20
    , b = letters[1:20]) %>%
    kable("latex", booktabs = TRUE) %>% # This already puts it on a separate page
    kable_styling(latex_options = c("striped"))
```
Run Code Online (Sandbox Code Playgroud)