What R packets use some random process when attaching?

The following answer to this question:

Why does package R load random numbers?

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

+1
source share
1 answer

I do not know how to be absolutely sure that you have made an exhaustive list. However, we can check all installed packages on your system to see if it has a .onAttach() or .onLoad() function, and if so, does it runif() or sample() , which, in my opinion, there will be the most common cases of a package having some kind of random process when joining. 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 led to the following:

 [1] "forecast" "ggplot2" 

As we see in my answer to the question you linked, ggplot2 does this to randomly select hints to display to the user. As it turned 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 You can easily adapt the template in the grepl() call above to add arbitrary functions that pseudo random number generators call.
+1
source

Source: https://habr.com/ru/post/1276239/


All Articles