Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Created November 11, 2023 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vjcitn/8959e01892df2cd5376ec8b8b63eaf73 to your computer and use it in GitHub Desktop.
Save vjcitn/8959e01892df2cd5376ec8b8b63eaf73 to your computer and use it in GitHub Desktop.
a redefinition of library() to be quieter
# a replacement for library() that does not print tons of messages
# as each package is loaded. place in .Rprofile
libstats = function(inisess, newsess) {
inibase = inisess$basePkgs # unchanging?
inioth = names(inisess$otherPkgs)
newbase = newsess$basePkgs
newoth = names(newsess$otherPkgs)
iniatt = length(unique(c(inibase,inioth)))
newatt = length(unique(c(newbase,newoth)))
addatt = newatt-iniatt
inilo = names(inisess$loadedOnly)
newlo = names(newsess$loadedOnly)
addlo = length(setdiff(newlo, inilo))
c(addatt=addatt, addlo=addlo)
}
library = function(...) {
libstats = function(inisess, newsess) {
inibase = inisess$basePkgs # unchanging?
inioth = names(inisess$otherPkgs)
newbase = newsess$basePkgs
newoth = names(newsess$otherPkgs)
iniatt = length(unique(c(inibase,inioth)))
newatt = length(unique(c(newbase,newoth)))
addatt = newatt-iniatt
inilo = names(inisess$loadedOnly)
newlo = names(newsess$loadedOnly)
addlo = length(setdiff(newlo, inilo))
c(addatt=addatt, addlo=addlo)
}
inisess = utils::sessionInfo()
suppressPackageStartupMessages({
libdata = base::library(...)
newsess = utils::sessionInfo()
lstats = libstats(inisess=inisess, newsess=newsess)
message(sprintf("%d/%d packages newly attached/loaded, see sessionInfo() for details.", lstats["addatt"],
lstats["addlo"]))
invisible(NULL)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment