R suppress startupMessages from dependency
Asked Answered
E

3

45

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 suppressPackageStartupMessages(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.

English answered 8/6, 2011 at 13:52 Comment(0)
A
27

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.

Actuality answered 8/6, 2011 at 14:27 Comment(0)
W
46

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.

Wolfgang answered 8/6, 2011 at 13:58 Comment(4)
The offending package does use packageStatupMessage. I am just looking for a fix that doesn't require me to take the offending package out of my Depends since it causes a warning when calling R CMD check.English
@English using Imports and namespaces doesn't result in warnings, and allows you to take the offending package out of Depends.Actuality
I need something even stronger > suppressMessages(library(lasso2)) R Package to solve regression problems while imposing an L1 constraint on the parameters. Based on S-plus Release 2.1 Copyright (C) 1998, 1999 Justin Lokhorst <[email protected]> Berwin A. Turlach <[email protected]> Bill Venables <[email protected]> Copyright (C) 2002 Martin Maechler <[email protected]>Nihil
Well ... in cases like this, and for local use, I have in the past simply downloaded the code and commented out the message. Won't help you for customer sites which install from CRAN though.Wolfgang
A
27

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.

Actuality answered 8/6, 2011 at 14:27 Comment(0)
A
0

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
> 
Axinomancy answered 3/3, 2020 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.