I am trying to code a set of functions to check for missing R packages, and to install them if necessary. There's some good code to do that at StackOverflow: start here.
I would like to make the functions as silent as possible, especially since R returns even successful messages in red ink. Accordingly, I have tried to pass the quietly = TRUE
argument to both library
and require
.
However, these options never seem to work:
# attempt to get a silent fail
require(xyz, quietly = TRUE)
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘xyz’
How can I get require
to fail silently, and what am I not getting about the quietly
option?
The documentation says:
quietly
a logical. If TRUE, no message confirming package loading is printed, and most often, no errors/warnings are printed if package loading fails.
But it seems to me "most often" should be "almost never" in my personal experience. I would happily hear about your experience with that. Rationale: coding functions to help students.
Add. The same question applies to quiet = TRUE
in install.packages()
. It kills only the progress bar, but not the "downloaded binary packages are in" message (printed in black, yay!) that follows, even though it has no use to the median user.
Add. In case this might be of interest to anyone, the code so far:
## getPackage(): package loader/installer
getPackage <- function(pkg, load = TRUE, silent = FALSE, repos = "http://cran.us.r-project.org") {
if(!suppressMessages(suppressWarnings(require(pkg, character.only = TRUE, quietly = TRUE)))) {
try(install.packages(pkg, repos = repos), silent = TRUE)
}
if(load) suppressPackageStartupMessages(library(pkg, character.only = TRUE, quietly = TRUE))
if(load & !silent) message("Loaded ", pkg)
}
## Not run:
x <- c("ggplot2", "devtools") # etc.
lapply(x, getPackage, silent = TRUE)
I'm thinking of just quitting the effort to use quietly
in the function above, given that it does not seem to work when expected. I should probably ask the R userlists about that to get an explanation from the core devteam. The suppressMessages(suppressWarnings(require(...)))
workaround can be unstable in my experience.
If you want to make your require
function very quiet, you can use :
suppressMessages(suppressWarnings(require(xyz)))
Which gives :
...well, nothing :)
I'm not sure exactly when this was added to the language, but the preferred way to do this nowadays is
suppressPackageStartupMessages({
require(this)
require(that)
...
})
This zorches "Loading ...", complaints about masking symbols, and other noise, but messages indicating actual problems still appear (e.g. a package or dependency is unavailable).
The simplest solution appears to be
try(library(xyz), silent=TRUE)
require
is basically a wrapper around tryCatch(library)
, so this just cuts out some extraneous code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With