如何在R Markdown中全局使用set.seed()?

Zac*_*yST 6 markdown r r-markdown

有人可以提供一个在R Markdown中全局使用set.seed()的工作示例吗?我知道基于此bug报告Yihui文档,但是当我将建议的选项放在安装块中时,我收到一条错误消息。 knitr::opts_chunk$set(cache.extra = rand_seed)

我想念什么?我目前在需要它的第一个代码块中只有一个随机种子,但是以后的块应该使用相同的种子。

[更新如下]

我的设置块:

```{r setup, include=FALSE}
#knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = "/Users/Zack/Documents/UCLA/Courses/PP290_NetworkScience")
#library(knitr)
knitr::opts_chunk$set(tidy.opts=list(width.cutoff=80),tidy=TRUE)
knitr::opts_chunk$set(cache.extra = rand_seed)
```
Run Code Online (Sandbox Code Playgroud)

错误:

Show in New WindowClear OutputExpand/Collapse Output
Error in knitr::opts_chunk$set(cache.extra = rand_seed) : 
  object 'rand_seed' not found
Run Code Online (Sandbox Code Playgroud)

使用种子的块是这样的:

```{r section1_3, error=TRUE, cache=FALSE, eval=TRUE, echo=TRUE}
set.seed(01082017)
# A binomial distribution is one that produces a series of numbers according to parameters you pass it.
# We can easily make it produces 1s and 0s and then populate an adjacency matrix with them.
# The last argument controls the ratio of 1s and 0s.  So, half the output will be 1, half will be 0, on average.
rbinom(1,1,.5)
rbinom(1,1,.5)
rbinom(1,1,.5)
rbinom(1,1,.5)
rbinom(1,1,.5)
```
Run Code Online (Sandbox Code Playgroud)

目前,我已经knitr::opts_chunk$set(cache.extra = rand_seed)从我的R Markdown文件中删除了。

Mat*_*alZ 0

```{r setup, include=FALSE}
#knitr::opts_chunk$set(echo = TRUE) knitr::opts_knit$set(root.dir = 
"/Users/Zack/Documents/UCLA/Courses/PP290_NetworkScience")
#library(knitr) 
knitr::opts_chunk$set(tidy.opts=list(width.cutoff=80),tidy=TRUE) 
knitr::opts_chunk$set(cache.extra = rand_seed)
```
Run Code Online (Sandbox Code Playgroud)

您是否尝试取消注释库(knitr)?