如何使用knitr和RMarkdown在一个块中的多个页面上添加多个数字?

Ban*_*you 11 r rstudio knitr r-markdown

我正在使用for循环在一个块中使用knitr和rmarkdown在多个页面上创建多个大数字.它适用于word和html输出,但在pdf输出中有一个问题.

这是重现我的问题的最小RMarkdown示例.

---
title: "Knitr test"
date: "6 April 2015"
output: pdf_document
---


```{r, echo=FALSE, fig.width=6.5,fig.height=10}
library(ggplot2)
for (i in seq(1, 4)){
    p <- ggplot(cars, aes(speed, dist)) + geom_point() 
    print(p)
}
```
Run Code Online (Sandbox Code Playgroud)

生成的pdf文件如下所示.页面中打印了两个数字.

在此输入图像描述

如果我更改了fig.height,在rmd文件中添加一些部分,仍然会在同一页面中打印两个不同排列的数字.

---
title: "Knitr test"
output: pdf_document
date: "6 April 2015"
---

## Section A

Row B

```{r plot_phenotype, echo = FALSE, fig.height=8, fig.width=6.5}
library(ggplot2)
for (i in seq(1, 4))
{
    p <- ggplot(cars, aes(speed, dist)) + geom_point()
    print(p)
}

```
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

感谢您提出解决此问题的任何建议.

我使用的是RStudio 0.99.375.这是我的会话信息.

sessionInfo()
R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

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

other attached packages:
[1] rmarkdown_0.5.3.1 knitr_1.9.5      

loaded via a namespace (and not attached):
 [1] colorspace_1.2-5 digest_0.6.8     evaluate_0.5.5   formatR_1.0     
 [5] ggplot2_1.0.0    grid_3.1.3       gtable_0.1.2     htmltools_0.2.6 
 [9] MASS_7.3-34      munsell_0.4.2    plyr_1.8.1       proto_0.3-10    
[13] Rcpp_0.11.5      reshape2_1.4.1   scales_0.2.4     stringr_0.6.2   
[17] tcltk_3.1.3      tools_3.1.3      yaml_2.1.13     
Run Code Online (Sandbox Code Playgroud)

Ban*_*you 14

我已经解决了我的问题.

在生成的tex文件中,每个图后面没有新行.这个tex代码我们使用上面的rmd文件生成:

\includegraphics{test_files/figure-latex/plot_phenotype-1.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-2.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-3.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-4.pdf}
Run Code Online (Sandbox Code Playgroud)

解决方案是在每个循环后添加一个新行以打印图形.

cat('\r\n\r\n')
Run Code Online (Sandbox Code Playgroud)

不知道为什么我需要两个"\ r \n".生成的tex文件如下所示:

\includegraphics{test_files/figure-latex/plot_phenotype-1.pdf}

\includegraphics{test_files/figure-latex/plot_phenotype-2.pdf}

\includegraphics{test_files/figure-latex/plot_phenotype-3.pdf}

\includegraphics{test_files/figure-latex/plot_phenotype-4.pdf}
Run Code Online (Sandbox Code Playgroud)

这是我的Rmd文件的完整示例

---
title: "Knitr test"
output:
  pdf_document:
    keep_tex: yes
date: "6 April 2015"
---

## Section A

Row B

```{r plot_phenotype, echo = FALSE, fig.height=8, fig.width=6.5}
library(ggplot2)
library(grid)
for (i in seq(1, 4))
{
    grid.newpage()
    p <- ggplot(cars, aes(speed, dist)) + geom_point()

    print(p)
    cat('\r\n\r\n')
}


```
Run Code Online (Sandbox Code Playgroud)


Pet*_*mis 7

编辑1(LaTeX):我似乎无法使用RMarkdown,因为它有新页面的问题.但是使用纯粹的LaTeX似乎解决了单页中的多个图和开头的文本的问题.不确定它是否是你想要的.在RStudio中打开一个新的R Sweave(.Rnw)文件并尝试:

\documentclass{article}
\begin{document}
\title{A report}
\author{Me}
\maketitle
\section{One section}
Some text that does not say anything interesting.

<<r, echo=FALSE, fig.width=6.5, fig.height=7>>=
library(ggplot2)
for (i in seq(1, 4)){
  p <- ggplot(cars, aes(speed, dist)) + geom_point()
  print(p)
}
@

\end{document}
Run Code Online (Sandbox Code Playgroud)

这产生以下结果:

只是一个pdf

编辑2(RMarkdown)(基于你的答案):运行你的答案中的代码我得到带有文本的第一页,然后是每个情节的单页.如果这是你想要的,你可以简单地使用(即删除grid.newpage,\rs和keep_tex)获得相同的结果:

---
title: "Knitr test"
output: pdf_document
date: "6 April 2015"
---

## Section A

Row B

```{r plot_phenotype, echo = FALSE, fig.height=10, fig.width=6.5}
library(ggplot2)
for (i in seq(1, 4))
{
    p <- ggplot(cars, aes(speed, dist)) + geom_point()

    print(p)
    cat('\n\n')
}


```
Run Code Online (Sandbox Code Playgroud)

产生以下内容:

其他截图

如果将fig.height更改为7,您将获得与上述LaTeX相同的结果.