【发布时间】:2017-12-20 01:07:35
【问题描述】:
有人可以提供一个如何在 R Markdown 中全局使用 set.seed() 的工作示例吗?我知道基于this bug report 的Yihui's documentation,但是当我将建议的选项作为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,我也会得到可重现的结果。 -
已更新。我是否将 set.seed() 放入设置块中?
-
我知道这是一个老问题,但我不清楚文档。我添加的信息有帮助吗?
标签: r markdown r-markdown