library                 package:base                 R Documentation

_L_o_a_d_i_n_g _a_n_d _L_i_s_t_i_n_g _o_f _P_a_c_k_a_g_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     'library' and 'require' load add-on packages.

     '.First.lib' is called when a package is loaded; '.Last.lib' is
     called when a package is detached.

_U_s_a_g_e:

     library(package, help, pos = 2, lib.loc = NULL,
             character.only = FALSE, logical.return = FALSE,
             warn.conflicts = TRUE,
             keep.source = getOption("keep.source.pkgs"),
             verbose = getOption("verbose"),
             version)

     require(package, lib.loc = NULL, quietly = FALSE, warn.conflicts = TRUE,
             keep.source = getOption("keep.source.pkgs"),
             character.only = FALSE, version, save = TRUE)

     .First.lib(libname, pkgname)
     .Last.lib(libpath)

_A_r_g_u_m_e_n_t_s:

package, help: the name of a package, given as a name or literal
          character string, or a character string, depending on whether
          'character.only' is 'FALSE' (default) or 'TRUE').

     pos: the position on the search list at which to attach the loaded
          package.  Note that '.First.lib' may attach other packages,
          and 'pos' is computed _after_ '.First.lib' has been run.  Can
          also be the name of a position on the current search list as
          given by 'search()'.

 lib.loc: 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. Non-existent
          library trees are silently ignored.

character.only: a logical indicating whether 'package' or 'help' can be
          assumed to be character strings.

 version: A character string denoting a version number of the package
          to be loaded, for use with _versioned installs_: see the
          section later in this document.

logical.return: logical.  If it is 'TRUE',  'FALSE' or 'TRUE' is
          returned to indicate success.

warn.conflicts: logical.  If 'TRUE', warnings are printed about
          'conflicts' from attaching the new package, unless that
          package contains an object '.conflicts.OK'.  A conflict is a
          function masking a function, or a non-function masking a
          non-function. 

keep.source: logical.  If 'TRUE', functions "keep their source"
          including comments, see argument 'keep.source' to 'options'. 
          This applies only to the named package, and not to any
          packages or namespaces which might be loaded to satisfy
          dependencies or imports.

          This argument does not apply to packages using lazy-loading
          or saved images.  Whether they have kept source is determined
          when they are installed (and is most likely false). 

 verbose: a logical.  If 'TRUE', additional diagnostics are printed.

 quietly: a logical.  If 'TRUE', no message confirming package loading
          is printed.

    save: logical or environment.  If 'TRUE', a call to 'require' from
          the source for a package will save the name of the required
          package in the variable '".required"', allowing function
          'detach' to warn if a required package is detached. See
          section 'Packages that require other packages' below.

 libname: a character string giving the library directory where the
          package was found.

 pkgname: a character string giving the name of the package, including
          the version number if the package was installed with
          '--with-package-versions'.

 libpath: a character string giving the complete path to the package.

_D_e_t_a_i_l_s:

     'library(package)' and 'require(package)' both load the package
     with name 'package'.  'require' is designed for use inside other
     functions; it returns 'FALSE' and gives a warning (rather than an
     error as 'library()' does by default) if the package does not
     exist.  Both functions check and update the list of currently
     loaded packages and do not reload a package which is already
     loaded.

     If 'library' is called with no 'package' or 'help' argument, it
     lists all available packages in the libraries specified by
     'lib.loc', and returns the corresponding information in an object
     of class '"libraryIQR"'.  The structure of this class may change
     in future versions.  In earlier versions of R, only the names of
     all available packages were returned; use '.packages(all = TRUE)'
     for obtaining these.  Note that 'installed.packages()' returns
     even more information.

     'library(help = somename)' computes basic information about the
     package 'somename', and returns this in an object of class
     '"packageInfo"'.  The structure of this class may change in future
     versions.  When used with the default value ('NULL') for
     'lib.loc', the loaded packages are searched before the libraries.

     '.First.lib' is called when a package without a namespace is
     loaded by 'library'.  (For packages with namespaces see
     '.onLoad'.)  It is called with two arguments, the name of the
     library directory where the package was found (i.e., the
     corresponding element of 'lib.loc'), and the name of the package
     (in that order, and with the package name including the version
     for a versioned install, e.g. 'tree_1.0-16').  It is a good place
     to put calls to 'library.dynam' which are needed when loading a
     package into this function (don't call 'library.dynam' directly,
     as this will not work if the package is not installed in a
     "standard" location).  '.First.lib' is invoked after the search
     path interrogated by 'search()' has been updated, so
     'as.environment(match("package:name", search()))' will return the
     environment in which the package is stored.  If calling
     '.First.lib' gives an error the loading of the package is
     abandoned, and the package will be unavailable.  Similarly, if the
     option '".First.lib"' has a list element with the package's name,
     this element is called in the same manner as '.First.lib' when the
     package is loaded.  This mechanism allows the user to set package
     "load hooks" in addition to startup code as provided by the
     package maintainers, but 'setHook' is preferred.

     '.Last.lib' is called when a package is detached.  Beware that it
     might be called if '.First.lib' has failed, so it should be
     written defensively. (It is called within 'try', so errors will
     not stop the package being detached.)

_V_a_l_u_e:

     'library' returns the list of loaded (or available) packages (or
     'TRUE' if 'logical.return' is 'TRUE'). 'require' returns a logical
     indicating whether the required package is available.

_P_a_c_k_a_g_e_s _t_h_a_t _r_e_q_u_i_r_e _o_t_h_e_r _p_a_c_k_a_g_e_s:

     *NB:* This mechanism has been almost entirely superseded by using
     the 'Depends:' field in the 'DESCRIPTION' file of a package.

     The source code for a package that requires one or more other
     packages should have a call to 'require', preferably near the
     beginning of the source, and of course before any code that uses
     functions, classes or methods from the other package.  The default
     for argument 'save' will save the names of all required packages
     in the environment of the new package.  The saved package names
     are used by 'detach' when a package is detached to warn if other
     packages still require the to-be-detached package.  Also, if a
     package is installed with saved image (see INSTALL), the saved
     package names are used to require these packages when the new
     package is attached.

_F_o_r_m_a_l _m_e_t_h_o_d_s:

     'library' takes some further actions when package 'methods' is
     attached (as it is by default).  Packages may define formal
     generic functions as well as re-defining functions in other
     packages (notably 'base') to be generic, and this information is
     cached whenever such a package is loaded after 'methods' and
     re-defined functions are excluded from the list of conflicts.  The
     check requires looking for a pattern of objects; the pattern
     search may be avoided by defining an object '.noGenerics' (with
     any value) in the package.  Naturally, if the package _does_ have
     any such methods, this will prevent them from being used.

_V_e_r_s_i_o_n_e_d _i_n_s_t_a_l_l_s:

     Packages can be installed with version information by 'R CMD
     INSTALL --with-package-versions' or
     'install.packages(installWithVers = TRUE)'.  This allows more than
     one version of a package to be installed in a library directory,
     using package directory names like 'foo_1.5-1'.  When such
     packages are loaded, it is this _versioned_  name that 'search()'
     returns.  Some utility functions require the versioned name and
     some the unversioned name (here 'foo').

     If 'version' _is not_ specified, 'library' looks first for a
     packages of that name, and then for versioned installs of the
     package, selecting the one with the latest version number.  If
     'version' _is_ specified, a versioned install with an exactly
     matching version is looked for.

     If 'version' is not specified, 'require' will accept any version
     that is already loaded, whereas 'library' will look for an
     unversioned install even if a versioned install is already loaded.

     Loading more than one version of a package into an R session is
     not currently supported.  Support for versioned installs is
     patchy.

_N_o_t_e:

     'library' and 'require' can only load an _installed_ package, and
     this is detected by having a 'DESCRIPTION' file containing a
     'Built:' field.

     Under Unix-alikes, the code checks that the package was installed
     under a similar operating system as given by 'R.version$platform'
     (the canonical name of the platform under which R was compiled),
     provided it contains compiled code.  Packages which do not contain
     compiled code can be shared between Unix-alikes, but not to other
     OSes because of potential problems with line endings and
     OS-specific help files.

     As of version 2.0.0, the package name given to 'library' and
     'require' must match the name given in the package's 'DESCRIPTION'
     file exactly, even on case-insensitive file systems such as MS
     Windows.

_R_e_f_e_r_e_n_c_e_s:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_. Wadsworth & Brooks/Cole.

_S_e_e _A_l_s_o:

     '.libPaths', '.packages'.

     'attach', 'detach', 'search', 'objects', 'autoload',
     'library.dynam', 'data', 'install.packages' and
     'installed.packages'; 'INSTALL', 'REMOVE'.

_E_x_a_m_p_l_e_s:

     library()                   # list all available packages
     library(lib = .Library)     # list all packages in the default library
     library(help = splines)     # documentation on package 'splines'
     library(splines)            # load package 'splines'
     require(splines)            # the same
     search()                    # "splines", too
     detach("package:splines")

     # if the package name is in a character vector, use
     pkg <- "splines"
     library(pkg, character.only = TRUE)
     detach(pos = match(paste("package", pkg, sep=":"), search()))

     require(pkg, character.only = TRUE)
     detach(pos = match(paste("package", pkg, sep=":"), search()))

     require(nonexistent)        # FALSE
     ## Not run: 
     ## Suppose a package needs to call a shared library named 'fooEXT',
     ## where 'EXT' is the system-specific extension.  Then you should use
     .First.lib <- function(lib, pkg) {
       library.dynam("foo", pkg, lib)
     }

     ## if you want to mask as little as possible, use
     library(mypkg, pos = "package:base")
     ## End(Not run)

