请给出一个在R Markdown中如何全局使用set.seed()的实际工作示例。我知道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)
```
错误:
Show in New WindowClear OutputExpand/Collapse Output
Error in knitr::opts_chunk$set(cache.extra = rand_seed) :
object 'rand_seed' not found
使用种子的代码块如下:
```{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)
```
目前,我已从我的R Markdown文件中删除了knitr :: opts_chunk $ set(cache.extra = rand_seed)
。
cache=TRUE
,我也能得到可重现的结果。 - hrbrmstrrand_seed
is exported fromknitr
, so you probably need to runknitr::rand_seed
instead of justrand_seed
- David Rubinger