Slovník | Vyhledávání | Mapa webu
 
Základy informatiky pro biologyCvičebnice jazyka R Dokumentace k základním funkcím library

library

Výčet, načtení a připojení balíku

library {base} Dokumentace R v češtině

Popis

library nebo require načítá a připojuje přídavné balíky.

Použití

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

require(package, lib.loc = NULL, quietly = FALSE,
warn.conflicts = TRUE,
character.only = FALSE)

Argumenty

package, help

název balíku, který může být zadán jako name, literál nebo znakový řetězec podle toho, zda character.only je FALSE (default) nebo TRUE.

pos

pozice v seznamu hledání, ke které chcete připojit načtený jmenný prostor. Může také být jméno pozice v aktuálním seznamu hledání zadané jako search().

lib.loc

je buď znakový vecktor popisující umístění R složky, ve které mají být vyhledány balíky, nebo NULL. Výchozí hodnota NULL umožní vyhledat všechny balíky, které jsou aktuálně v .libPaths(). Neexistující složky s balíky jsou ignorovány bez hlášení.

character.only

logická proměnná, která rozhodne, zda package či help ma být brán jako řetězec znaků.

logical.return

pokud je tato logická proměnná TRUE, vrátí FALSE nebo TRUE v závisloti na tom, zda je balík připojen či nikoliv.

warn.conflicts

Jedná se o logickou proměnnou. Pokud nabývá TRUE, zobrazí se varovná zpráva o conflicts při připojování nového balíku. Konfliktem se rozumí situace, kdy funkce z nového balíku maskuje funkci se stejným názvem z již existujícího balíku. Může se jednat i o jiné objekty (např. proměnné či datové typy).

verbose

logická proměnná. Pokud je TRUE, zobrazí se přídatné diagnostické zprávy.

quietly

Je-li tato logická proměnná TRUE, nezobrazí se při přidání nového balíku žádná konfirmační zpráva. Rovněž se nezobrazí ani žádné chybové hlášení v případě, že přidání baílu nebylo úspěšné.

Detaily

library(package) and require(package) both load the namespace of the package with name package and attach it on the search list. 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 attached packages and do not reload a namespace which is already loaded. (If you want to reload such a package, call detach(unload = TRUE) or unloadNamespace first.) If you want to load a package without attaching it on the search list, see requireNamespace.

To suppress messages during the loading of packages use suppressPackageStartupMessages: this will suppress all messages from R itself but not necessarily all those from package authors.

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.) Use .packages(all = TRUE) to obtain just the names of all available packages, and installed.packages() for 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 attached packages are searched before the libraries.

Value

Normally library returns (invisibly) the list of attached packages, but TRUE or FALSE if logical.return is TRUE. When called as library() it returns an object of class "libraryIQR", and for library(help=), one of class "packageInfo".

require returns (invisibly) a logical indicating whether the required package is available.

Licenses

Some packages have restrictive licenses, and there is a mechanism to allow users to be aware of such licenses. If getOption("checkPackageLicense") == TRUE, then at first use of a package with a not-known-to-be-FOSS (see below) license the user is asked to view and accept the license: a list of accepted licenses is stored in file ‘~/.R/licensed’. In a non-interactive session it is an error to use such a package whose license has not already been recorded as accepted.

As from R 3.4.0 the license check is done when the namespace is loaded.

Free or Open Source Software (FOSS, e.g. https://en.wikipedia.org/wiki/FOSS) packages are determined by the same filters used by available.packages but applied to just the current package, not its dependencies.

There can also be a site-wide file ‘R_HOME/etc/licensed.site’ of packages (one per line).

Formal methods

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 namespace is loaded after methods and re-defined functions (implicit generics) are excluded from the list of conflicts. The caching and check for conflicts require looking for a pattern of objects; the search may be avoided by defining an object .noGenerics (with any value) in the namespace. Naturally, if the package does have any such methods, this will prevent them from being used.

Note

library and require can only load/attach 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. If sub-architectures are used, the OS similarity is not checked since the OS used to build may differ (e.g. i386-pc-linux-gnu code can be built on an x86_64-unknown-linux-gnu OS).

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 are common on Windows and macOS.

References

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

See Also

.libPaths, .packages.

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

The initial set of packages attached is set by options(defaultPackages=): see also Startup.

Examples

library() # list all available packages
library(lib.loc = .Library) # list all packages in the default library
library(help = splines) # documentation on package 'splines'
library(splines) # attach 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:
## if you want to mask as little as possible, use
library(mypkg, pos = "package:base")

## End(Not run)


[Package base version 3.6.0 Index]
 
vytvořil Institut biostatistiky a analýz Lékařské fakulty Masarykovy univerzity