R suppress startupMessages from dependency

后端 未结 3 1684
予麋鹿
予麋鹿 2020-11-27 19:19

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

相关标签:
3条回答
  • 2020-11-27 19:56

    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.

    0 讨论(0)
  • 2020-11-27 20:05

    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
    > 
    
    0 讨论(0)
  • 2020-11-27 20:06

    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.

    0 讨论(0)
提交回复
热议问题