2

I have put some of the functions that I use regularly into one package with roxygen2 and everything is working smoothly. The only thing that bothers me is that upon loading the package I get a wall of text from all the loaded dependencies printed into the console. I know that I can supress warnings when I use suppressWarnings(suppressMessages(library("PACKAGE"))) but I am wondering if I can also set this option directly in the package (maybe in NAMESPACE)? The idea would be that the warnings from the dependencies are suppressed and I can specify my own text that is displayed when the package is loaded.

I tried the suggestion of @NelsonGon and added this to a file called zzz.R. The additional welcome text is printed as it should be but the loading messages from the dependcies are still printed.

# Welcome message
.onLoad <- function(...){

  invisible(suppressPackageStartupMessages(
    sapply(c("stringi", "stringr",
             "qdapRegex", "readr",
             "tokenizers", "rvest",
             "pryr", "XML", "xml2",
             "lubridate", "data.table",
             "ggplot2", "anytime", "dplyr",
             "network", "quanteda", "ggmap",
             "networkDynamic", "mgsub",
             "dplyr", "ggplot2", "network",
             "stats", "ndtv", "devtools",
             "ggtext"),
           requireNamespace, quietly = TRUE)))

  pkg_info <- "Welcome to my package"
  packageStartupMessage(pkg_info)

}
Ju Ko
  • 466
  • 7
  • 22
  • Does this answer your question? [R package development how to suppress messages generated from dependency package?](https://stackoverflow.com/questions/53941218/r-package-development-how-to-suppress-messages-generated-from-dependency-package) – NelsonGon Jun 29 '20 at 13:43
  • 1
    @NelsonGon This is what I want to do, but I'm using roxygen2 to build the package, so I'm not sure where I'd have to put the suggested changes, I never explicitly used `devtools::load_all` or `onLoad`. Do you know where I can edit these when the package is build with roxygen2? – Ju Ko Jun 29 '20 at 14:09
  • 1
    Put `.onLoad` in `zzz.R`. You can see an example on my [repo](https://github.com/Nelson-Gon/manymodelr/blob/master/R/zzz.R). `load_all`(only needed to test changes as you build) is irrelevant. Just put `suppressPackageStartupMessages` as stated there ie just copy `.onLoad` to `zzz.R`. – NelsonGon Jun 29 '20 at 14:11
  • I tried this out and the text I put into zzz.R is printed when loading the function but the loadup text from the dependencies is still printed aswell. – Ju Ko Jun 30 '20 at 08:01
  • Could you link to your file? – NelsonGon Jun 30 '20 at 10:49
  • 1
    I can confirm that it doesn't work. – NelsonGon Jun 30 '20 at 11:36
  • 4
    Are you using the `Depends` field of the `DESCRIPTION` file? Normally one uses the `Imports` field, and there's no startup messages. – Stéphane Laurent Jul 09 '20 at 12:14
  • 2
    Presumably this is a package for local use? If I was an end user I would certainly want to know that 26 sizeable packages were being loaded and added to my search path. Even if it is for local use, I would do as @StéphaneLaurent suggests and put the packages in `Imports` rather than `Depends`, using namespace qualifiers to refer to imported functions in my code. – Allan Cameron Jul 09 '20 at 13:04
  • Since packages can use any variant of `packageStartupMessage`, `message`, and `cat` (if they're being nice), I see you've mentioned `suppressWarnings(suppressMessages(...))` and separately `suppressPackageStartupMessages`, what about all three? They are all different kinds of "messages". – r2evans Jul 10 '20 at 22:46
  • ... and perhaps `capture.output` for good measure (or overkill). – r2evans Jul 10 '20 at 23:00

1 Answers1

1

Under the hood, package loading calls :
pkgload::load_depends which calls
require which has a quietly = FALSE default option.

You just need to set this option to TRUE to avoid messages :

# Welcome message
.onLoad <- function(...){
 quietly <- getOption('quietly')
 options(quietly = T)
 pkg_info <- "Welcome to my package"
 packageStartupMessage(pkg_info)
 options(quietly = quietly)
}
> devtools::load_all(".")
Loading myPackage
Welcome to my package
Waldi
  • 39,242
  • 6
  • 30
  • 78
  • I tried this but I still get the same messagtes printed to the console when I load the package. – Ju Ko Jul 15 '20 at 12:37
  • 1
    do you get exactly the same messages, or some of them disappeared? I tried this with data.table and output was clean. – Waldi Jul 15 '20 at 12:40
  • My bad, I forgot to save the function! Just tried it out again and I get significantly less messages! The only thing I still get are warnings that some two S3 methods are overwritten. It seemed like a combination of your code and the suggestion of Stephane Laurent did the trick! – Ju Ko Jul 15 '20 at 12:44