caret
最近,当我注意到这一点时,我正在阅读包的文档:
另外,请注意,某些包在加载(直接或通过命名空间)时会加载随机数,这可能会影响 [原文如此] 可重复性。
加载随机数的包有哪些可能的用例?这似乎与可重复研究的想法背道而驰,并且可能会干扰我自己的尝试set.seed
。(我已经开始将种子设置为更接近需要随机数生成的代码,因为我担心加载包的副作用。)
caret
最近,当我注意到这一点时,我正在阅读包的文档:
另外,请注意,某些包在加载(直接或通过命名空间)时会加载随机数,这可能会影响 [原文如此] 可重复性。
加载随机数的包有哪些可能的用例?这似乎与可重复研究的想法背道而驰,并且可能会干扰我自己的尝试set.seed
。(我已经开始将种子设置为更接近需要随机数生成的代码,因为我担心加载包的副作用。)
ggplot2
正如Hadley Wickham在对与.tidyverse
当附上包裹时,会随机选择一个小费显示给用户(并且有一定的概率,不会显示小费)。如果我们检查它在 2018 年 1 月之前存在.onAttach()
的函数,我们会看到它同时调用了and ,改变了种子:runif()
sample()
.onAttach <- function(...) {
if (!interactive() || stats::runif(1) > 0.1) return()
tips <- c(
"Need help? Try the ggplot2 mailing list: http://groups.google.com/group/ggplot2.",
"Find out what's changed in ggplot2 at http://github.com/tidyverse/ggplot2/releases.",
"Use suppressPackageStartupMessages() to eliminate package startup messages.",
"Stackoverflow is a great place to get help: http://stackoverflow.com/tags/ggplot2.",
"Need help getting started? Try the cookbook for R: http://www.cookbook-r.com/Graphs/",
"Want to understand how all the pieces fit together? Buy the ggplot2 book: http://ggplot2.org/book/"
)
tip <- sample(tips, 1)
packageStartupMessage(paste(strwrap(tip), collapse = "\n"))
}
release_questions <- function() {
c(
"Have you built the book?"
)
}
然而,这已经通过由Jim Hester编写的提交修复,以便在附加后重置种子:ggplot2
.onAttach <- function(...) {
withr::with_preserve_seed({
if (!interactive() || stats::runif(1) > 0.1) return()
tips <- c(
"Need help? Try the ggplot2 mailing list: http://groups.google.com/group/ggplot2.",
"Find out what's changed in ggplot2 at http://github.com/tidyverse/ggplot2/releases.",
"Use suppressPackageStartupMessages() to eliminate package startup messages.",
"Stackoverflow is a great place to get help: http://stackoverflow.com/tags/ggplot2.",
"Need help getting started? Try the cookbook for R: http://www.cookbook-r.com/Graphs/",
"Want to understand how all the pieces fit together? Buy the ggplot2 book: http://ggplot2.org/book/"
)
tip <- sample(tips, 1)
packageStartupMessage(paste(strwrap(tip), collapse = "\n"))
})
}
因此,包执行此操作的原因可能有多种,尽管包作者可以通过多种方式防止这种情况给用户带来意想不到的后果。