Skip to content
Prev 40518 / 63424 Next

How to add a welcome message in Package development?

I agree with Simon that package startup messages
are often annoying and not often helpful.  However,
I think they should be generated with
   packageStartupMessage("your message")
instead of
   cat("your message\n")
The former may be suppressed by wrapping the call to
library() with suppressPackageStartupMessages(), just
as messages generated by message("your message") may
be suppressed with suppressMessages().

You can suppress cat()'s output with capture.output()
but you may throw out things that should be seen.

E.g., if a package with a NAMESPACE file contains
the following .onXXX functions
  % more testPkgWithNamespace/R/zzz.R
  .onAttach <- function(...) {
      cat("message from .onAttach via cat\n")
      message("message from .onAttach via message")
      packageStartupMessage("message from .onAttach via
packageStartupMessage\n")
  }
  .onLoad <- function(...) {
      cat("message from .onLoad via cat\n")
      message("message from .onLoad via message")
      packageStartupMessage("message from .onLoad via
packageStartupMessage\n")
  }
we get:
  > library(testPkgWithNamespace, lib.loc="Rlib")
  message from .onLoad via cat
  message from .onLoad via message
  message from .onLoad via packageStartupMessage
  message from .onAttach via cat
  message from .onAttach via message
  message from .onAttach via packageStartupMessage
  >
  > detach("package:testPkgWithNamespace", unload=TRUE)
  > suppressMessages(library(testPkgWithNamespace, lib.loc="Rlib"))
  message from .onLoad via cat
  message from .onAttach via cat
  >
  > detach("package:testPkgWithNamespace", unload=TRUE)
  > suppressPackageStartupMessages(library(testPkgWithNamespace,
lib.loc="Rlib"))
  message from .onLoad via cat
  message from .onLoad via message
  message from .onAttach via cat
  message from .onAttach via message

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com