How to create custom start-up messages for R packages?

For example, if I load the R package "mclust", I see

> library(mclust)
    __  ___________    __  _____________
   /  |/  / ____/ /   / / / / ___/_  __/
  / /|_/ / /   / /   / / / /\__ \ / /   
 / /  / / /___/ /___/ /_/ /___/ // /    
/_/  /_/\____/_____/\____//____//_/    version 5.4.7
Type 'citation("mclust")' for citing this R package in publications.

When building an R package myself, how can I create such a custom start-up message?

EDIT: To be clear, I do not ask how to create the ASCII art but how to display a startup message in general.

Upvotes: 6

Views: 1005

Answers (2)

Viet Than
Viet Than

Reputation: 320

To add on to the other excellent answer, we can find this exact example in the mclust project at path:

mclust/R/zzz.R

The file zzz.R contains the following:

# .onLoad <- function(libname, pkgname) 
# {
#   library.dynam("mclust", pkgname, libname)
# }

mclustStartupMessage <- function()
{
# Startup message obtained as 
# > figlet -f slant MCLUST
  msg <- c(paste0(
"    __  ___________    __  _____________
   /  |/  / ____/ /   / / / / ___/_  __/
  / /|_/ / /   / /   / / / /\\__ \\ / /   
 / /  / / /___/ /___/ /_/ /___/ // /    
/_/  /_/\\____/_____/\\____//____//_/    version ", 
packageVersion("mclust")),
"\nType 'citation(\"mclust\")' for citing this R package in publications.")
  return(msg)
}

.onAttach <- function(lib, pkg)
{
  # unlock .mclust variable allowing its modification
  unlockBinding(".mclust", asNamespace("mclust")) 
  # startup message
  msg <- mclustStartupMessage()
  if(!interactive())
    msg[1] <- paste("Package 'mclust' version", packageVersion("mclust"))
  packageStartupMessage(msg)      
  invisible()
}

Upvotes: 3

user2554330
user2554330

Reputation: 44877

Your question isn't completely clear. Are you asking about how to create the ASCII art showing MCLUST, or how to display a startup message? I'll assume the latter.

You add a function like

.onAttach <- function(libname, pkgname) {
  packageStartupMessage("This is version ", packageVersion(pkgname), 
                        " of ", pkgname)
}

somewhere in your package code. By convention, you'd put this in a file called R/zzz.R, but it can go anywhere in the R code.

Don't print things other than errors or warnings from .onLoad(); that should normally be silent.

Upvotes: 10

Related Questions