One of my R package's dependencies displays startup messages when loaded. I would like to suppress these startup messages.
The only fix I found so far was removing the offending package from the Depends: line in my DESCRIPTION file. Then calling suppressPackageStartupMe开发者_如何学Pythonssages(require("offendingPackage"))
in .onLoad
of my package.
I would rather keep the offending package as part of my Depends, but it seems that anything specified in depends is automatically loaded and therefore can't be supressed.
The suppressPackageStartupMessages()
function works if and only if the startup messages are actually written with packageStartupMessage()
-- see the help page.
Many packages just use cat()
, which one could consider a buglet. In that case
suppressMessages(library(foo))
works better.
If you work with namespaces, you can specify the package in Imports
, and load the necessary functions using import
or importFrom
. This way, the package is not attached, but the necessary functions can be loaded and used by your package. Without attaching, the startup messages are not given, so this approach assures you won't see any startup messages of packages specified in Imports.
Make sure you check that you imported everything that is of importance. If the package you import is dependent on other packages, I'm not sure everything you need to use those functions is imported. You might have to do a bit of puzzling to get everything you need loaded. On the plus side, using Imports
assures that any dependencies check will be carried out correctly.
Another option is to not specify the package in Depends
, but in Suggests
in the DESCRIPTION file, and use the option @Dirk gave you. This will give a correct dependency check if 'dependencies=TRUE' is set in install.packages()
. But personally I think using the namespaces is a lot more clean.
A quick hack to do this inline in a script or environment is to override library()/require()
to wrap the suppressPackageStartupMessages()
method:
> library(here) # This shows a message
here() starts at /home/z/development/
> require(here) # This shows a message
Loading required package: here
here() starts at /home/y
The workaround:
> flibrary <- library
> library <- function(...) suppressPackageStartupMessages(flibrary(...))
> library(here) # No messages
>
> frequire <- require
> require <- function(...) suppressPackageStartupMessages(frequire(...))
> require(here) # No messages
>
精彩评论