哪些 R 包在附加时使用某种随机过程?
Which R packages use some kind of random process when attached?
这个问题的后续:
我想知道是否有一种方法可以生成所有包的列表,这些包在附加时包含某种随机过程。
我不知道有什么方法可以绝对确定您已经列出了详尽的清单。但是,我们可以检查您系统上所有已安装的软件包,看看它是否有 .onAttach()
或 .onLoad()
函数,如果有,它是否调用 runif()
或 sample()
,我认为这是最常见的包在附加时具有某种随机过程的情况。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)
}
对我来说,结果如下:
[1] "forecast" "ggplot2"
正如我们在 to the question you linked, ggplot2
does this to randomly select tips to display to the user. As it turns out, forecast
中看到的那样:
> forecast:::.onAttach
function (...)
{
if (!interactive() || stats::runif(1) > 0.2)
return()
tips <- c("Use suppressPackageStartupMessages() to eliminate package startup messages.",
"Whosebug is a great place to get help on R issues:\n http://whosebug.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()
调用中的模式,以添加调用 pseudo-random 数字生成器的任意函数。
这个问题的后续:
我想知道是否有一种方法可以生成所有包的列表,这些包在附加时包含某种随机过程。
我不知道有什么方法可以绝对确定您已经列出了详尽的清单。但是,我们可以检查您系统上所有已安装的软件包,看看它是否有 .onAttach()
或 .onLoad()
函数,如果有,它是否调用 runif()
或 sample()
,我认为这是最常见的包在附加时具有某种随机过程的情况。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)
}
对我来说,结果如下:
[1] "forecast" "ggplot2"
正如我们在 ggplot2
does this to randomly select tips to display to the user. As it turns out, forecast
中看到的那样:
> forecast:::.onAttach
function (...)
{
if (!interactive() || stats::runif(1) > 0.2)
return()
tips <- c("Use suppressPackageStartupMessages() to eliminate package startup messages.",
"Whosebug is a great place to get help on R issues:\n http://whosebug.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()
调用中的模式,以添加调用 pseudo-random 数字生成器的任意函数。