gpt4 book ai didi

r Markdown 输出到 pdf : stop plot breaking up a code chunk

转载 作者:行者123 更新时间:2023-12-04 16:07:18 24 4
gpt4 key购买 nike

我正在将 r markdown 文件打印成 pdf。我已经尝试了各种输出规范,但绘图一直显示在段 [1] 和 [2] 的中间。

我希望所有代码都显示为一个 block ,稍后将在文档中调用绘图。

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

最佳答案

使用 block 选项 fig.show = 'hold' 在 block 的末尾显示 block 生成的所有图。这是一个示例 .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.

enter image description here

编辑:

另一个解决方案,更接近我认为您正在寻找的,是使用 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.

enter image description here

关于r Markdown 输出到 pdf : stop plot breaking up a code chunk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44250180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com