Learn R Programming

svMisc (version 1.2.3)

package: A (possibly) very silent and multi-package library()/require() function

Description

This function loads one or several R packages as silently as possible (with warn/message = FALSE) and it returns TRUE only if all packages are loaded successfully. If at least one loading fails, a short message is printed, by default. For all packages that were not found, an entry is recorded in .packages_to_install in SciViews:TempEnv, and that list can be automatically used by Install().

Usage

package(
  ...,
  stop = TRUE,
  message = stop,
  warn.conflicts = message,
  pos = 2L,
  lib.loc = NULL,
  verbose = getOption("verbose")
)

Arguments

...

The name of one or several R packages to load (character strings).

stop

If TRUE, issue an error in case the package(s) cannot be loaded.

message

Do we display introductory message of the package? If a package displays such a message, there is often a good reason. So, it is not a good idea to disable it in interactive sessions. However, in other contexts, like in non-interactive use, inside an R Markdown document, etc., it is more convenient not to display it.

warn.conflicts

As for library(): "logical. If TRUE, warnings are printed about conflicts from attaching the new package. A conflict is a function masking a function, or a non-function masking a non-function.

pos

As for library(): "the position on the search list at which to attach the loaded namespace. Can also be the name of a position on the current search list as given by search()". Only one position can be provided here, even if several packages, and they will be all inserted one after the other at the given position.

lib.loc

As for library(): "a character vector describing the location of R library trees to search through, or NULL. The default value of NULL corresponds to all libraries currently known to .libPaths(). Non-existent library trees are silently ignored".

verbose

A logical indicating if additional diagnostic messages are printed.

Value

TRUE if all packages are loaded correctly, FALSE otherwise, with a details attribute indicating which package was loaded or not.

See Also

require(), library(), Install()

Examples

Run this code
# NOT RUN {
# This should work...
if (package('tools', 'methods', stop = FALSE)) message("Fine!")
# ... but this not (note that there are no details here!)
if (!package('tools', 'badname', stop = FALSE)) message("Not fine!")
# }
# NOT RUN {
# Get an error
package('badname')
# }

Run the code above in your browser using DataLab