为什么R包会加载随机数?

Sea*_*igh 5 packages r reproducible-research random-seed

最近,caret当我注意到这个时,我正在阅读包的文档:

此外,请注意,某些软件包在加载时(直接或通过命名空间)加载随机数,这可能会影响[ sic ]再现性.

加载随机数的包有哪些可能的用例?这似乎与可重复研究的想法相反,可能会干扰我自己的尝试set.seed.(我已开始将种子设置为更接近需要随机数生成的代码,因为我担心加载包的副作用.)

duc*_*ayr 9

ggplot2正如Hadley Wickham在回答与GitHub相关的问题时提到的这样做的一个例子是tidyverse.

当附加包裹时,随机选择一个尖端以便为用户显示(并且有可能没有显示尖端).如果我们检查它在2018年1月之前存在的.onAttach()功能,我们会看到它同时调用,并且更改种子: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?"
  )
}
Run Code Online (Sandbox Code Playgroud)

但是,这已经通过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"))
  })
}
Run Code Online (Sandbox Code Playgroud)

因此,包可以做到这一点的原因有多种,尽管包作者可以通过各种方式防止这种情况给用户带来意想不到的后果.