如何在R Markdown中全局使用set.seed()? [英] How to use set.seed() globally in R Markdown?
问题描述
有人可以提供一个工作示例,说明如何在R Markdown中全局使用set.seed()吗?我知道基于 knitr :: opts_chunk $ set(cache.extra = rand_seed)
.
Can someone please provide a working example of how to use set.seed() globally in R Markdown? I am aware of Yihui's documentation based on this bug report, but I get an error message when I put the suggested option in my setup chunk as knitr::opts_chunk$set(cache.extra = rand_seed)
.
我想念什么?我目前在需要它的第一个代码块中只有一个随机种子,但是以后的块应该使用相同的种子.
What am I missing? I currently just have a random seed in the first code chunk that needs it, but later chunks should use the same 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
使用种子的块是这样的:
The chunk that uses a seed is this:
```{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)
.
For now, I have removed knitr::opts_chunk$set(cache.extra = rand_seed)
from my R Markdown file.
推荐答案
我遇到了同样的问题,我想我已经解决了...将种子放入第一个块中,然后使 cache =在
.希望对您有帮助! knitr
选项中为TRUE
I've just had the same problem and I think I've fixed it... put the seed inside the first chunk and make cache = TRUE
in the knitr
options. I hope it helps you!
{r, set.seed(333)}
knitr::opts_chunk$set(cache = T)
[your code here]
这篇关于如何在R Markdown中全局使用set.seed()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!