我正在将PDF标记文件打印到pdf。我已经尝试了各种输出规格,但是图表一直显示在段[1]和[2]的中间。
我希望所有代码都显示一个块,然后在文档中稍后调用该图。
```{r, include=TRUE, results='hide'}
# [1] There is code up here
# [plot interrupts the code chunk here] Scatter plot
plot1 = plot(df$var1, df$var)
# [2] More code below this point
```
答案 0 :(得分:4)
使用块选项fig.show = 'hold'
显示块末尾的块生成的所有图。这是一个示例.Rmd文件和输出。
---
title: Stop Plot Breakikng Up Code Chunk
output: pdf_document
---
The key is to use the chunk option `fig.show = 'hold'` so that all plots from
the chunk will will displayed at the end of the chunk.
```{r setup, include = FALSE, cache = FALSE}
library(knitr)
opts_chunk$set(fig.show = "hold")
```
We'll use the `mtcars` data set for the example.
```{r plot1, include = TRUE, results = "hide"}
mean(mtcars$wt)
mean(mtcars$mpg)
plot(mtcars$wt, mtcars$mpg)
var(mtcars$wt)
```
End of example.
另一个更接近我认为您正在寻找的解决方案是使用ref.label
来重用代码块。
---
title: Stop Plot Breaking Up Code Chunk
output: pdf_document
---
The key is to use the chunk option `fig.show = 'hold'` so that all plots from
the chunk will will displayed at the end of the chunk.
```{r setup, include = FALSE, cache = FALSE}
library(knitr)
opts_chunk$set(fig.show = "hold",
collapse = TRUE)
```
We will use the `mtcars` data set for the example.
```{r all_code}
```{r mean_code, ref.label = "means", echo = TRUE, results = "hide"}
```{r plot1_code, ref.label = "plot1", echo = TRUE, fig.show = "hide", fig.keep = "none"}
```{r var_code, ref.label = "var_wt", echo = TRUE, results = "hide"}
```
Description of a plot
```{r "plot1", echo = FALSE}
plot(mtcars$wt, mtcars$mpg)
```
More text.
Below here, chunks that are evaluated, but not shown.
```{r means, include = FALSE}
mean(mtcars$wt)
mean(mtcars$mpg)
```
```{r var_wt, include = FALSE}
var(mtcars$wt)
```
End of the example.
答案 1 :(得分:-1)
你可以写2个块:
## Title
Herebelow the code are not separated by plots :
```{r display_code_only, eval=FALSE}
plot(pressure)
plot(iris)
```
Herebelow the plot are not separated by code :
```{r display_plot_only, echo=FALSE}
plot(pressure)
plot(iris)
```