哪些R软件包在连接时使用某种随机过程? [英] Which R packages use some kind of random process when attached?

查看:172
本文介绍了哪些R软件包在连接时使用某种随机过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该问题的后续行动:

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

我想知道是否有一种方法可以生成所有附有某种随机过程的软件包的列表.

I wonder if there's a way to generate a list of all packages that include some kind of random process when attached.

推荐答案

我不知道一种绝对确定您已列出详尽清单的方法.但是,我们可以检查系统上所有已安装的软件包,以查看其是否具有.onAttach().onLoad()函数,如果具有,请调用runif()sample(),我认为这是最多的.包装的常见情况是在附加时具有某种随机过程. 1

I do not know of a way to be absolutely certain you've made an exhaustive list. However, we can check all of the installed packages on your system to see if it has a .onAttach() or .onLoad() function, and if so, whether it calls runif() or sample(), which I believe would be the most common cases of a package having some kind of random process when attached.1

check_packages <- function() {
    # get the names of all installed packages
    packs <- installed.packages()[ ,"Package"]
    # create an object to hold the names of packages that mess with the seed
    result <- character()
    # for each package
    for ( pack in packs ) {
        # see if it has an .onAttach or .onLoad function
        onattach <- try(getFromNamespace(".onAttach", pack), silent = TRUE)
        onload <- try(getFromNamespace(".onLoad", pack), silent = TRUE)
        # and if it does, check if it calls sample() or runif()
        if ( !inherits(onattach, "try-error") ) {
            if ( any(grepl("runif|sample", capture.output(onattach))) ) {
                # if so, add that package to the result object
                result <- c(result, pack)
                next()
            }
        }
        if ( !inherits(onload, "try-error") ) {
            if ( any(grepl("runif|sample", capture.output(onload))) ) {
                result <- c(result, pack)
                next()
            }
        }
    }
    # and return the names of all packages that do this
    return(result)
}

对我来说,这导致了以下结果:

For me, this resulted in the following:

[1] "forecast" "ggplot2" 

我们在我的答案中看到了您链接的问题. com/tidyverse/ggplot2"rel =" nofollow noreferrer> ggplot2 这样做是为了随机选择要显示给用户的提示.事实证明, forecast 的作用相同:

As we see in my answer to the question you linked, ggplot2 does this to randomly select tips to display to the user. As it turns out, forecast does the same:

> forecast:::.onAttach
function (...) 
{
    if (!interactive() || stats::runif(1) > 0.2) 
        return()
    tips <- c("Use suppressPackageStartupMessages() to eliminate package startup messages.", 
        "Stackoverflow is a great place to get help on R issues:\n  http://stackoverflow.com/tags/forecasting+r.", 
        "Crossvalidated is a great place to get help on forecasting issues:\n  http://stats.stackexchange.com/tags/forecasting.", 
        "Need help getting started? Try the online textbook FPP:\n  http://OTexts.org/fpp/", 
        "Want to stay up-to-date? Read the Hyndsight blog:\n  https://robjhyndman.com/hyndsight/", 
        "Want to meet other forecasters? Join the International Institute of Forecasters:\n  http://forecasters.org/")
    tip <- sample(tips, 1)
    msg <- paste("This is forecast", packageVersion("forecast"), 
        "\n ", tip)
    packageStartupMessage(msg)
}
<bytecode: 0x10e92738>
<environment: namespace:forecast>


1 您可以轻松地在上面的grepl()调用中调整模式,以添加调用伪随机数生成器的任意函数.


1 You could easily adapt the pattern in the grepl() call above to add arbitrary functions that call pseudo-random number generators.

这篇关于哪些R软件包在连接时使用某种随机过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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