为什么R包会加载随机数? [英] Why would an R package load random numbers?

查看:174
本文介绍了为什么R包会加载随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,当我注意到此内容时,我正在阅读caret软件包的文档:

Recently, I was reading the documentation for the caret package when I noticed this:

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

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

What are possible use cases for packages loading random numbers? This seems to be counter to the idea of reproducible research and might interfere with my own attempts to set.seed. (I've started setting seeds closer to code that requires random number generation precisely because I'm worried about the side effects of loading packages.)

推荐答案

执行此操作的程序包的一个示例是 ggplot2 ,如 Hadley Wickham 在对 tidyverse .

One example of a package that does this is ggplot2, as mentioned by Hadley Wickham in a response to a GitHub issue related to tidyverse.

包装时,将随机选择一个提示以显示给用户(并且有可能不显示提示).如果我们检查其.onAttach()函数在2018年1月之前存在的,我们看到它同时调用runif()sample(),更改了种子:

When the package is attached, a tip is randomly selected to be displayed for the user (and with some probability, no tip is displayed). If we examine its .onAttach() function as it existed before January 2018, we see it calls both runif() and sample(), changing the seed:

.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后重置种子:

However, this has since been fixed with a commit authored by Jim Hester so that the seed is reset after ggplot2 is attached:

.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"))
  })
}

因此,程序包可以这样做的原因可能有多种,尽管程序包作者可以通过多种方法来防止此操作给用户带来意想不到的后果.

So, there could be various reasons why a package does this, though there are ways that package authors can prevent this from giving unexpected consequences to the user.

这篇关于为什么R包会加载随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆