1.2 Loading libraries

Functions in R are bundled in and so-called “packages”. A basic installation of R already comes with a certain collection of base packages, which are separately stored in the “System Library”. No matter how fundemental or basic the function, it is always part of a package, even though often not immediately realised. For example, the common function to print something to the R console print() is part of the base package (see topmost line in ?print, which states “print {base}”).

When starting R (or rather RStudio) all these base functions like print() or sum() are immediately available to the user. This is because all packages in the System Library are automatically loaded when starting R. A great advantage of R, however, is that its functionality can be greatly expanded by sharing and including custom packages. After installing a package (like e.g. the Luminescence package) its functions are not immediately available to the user when starting R. When we call a function of a package that is not yet loaded we get an error message like the following:

calc_CentralDose()
## Error in calc_CentralDose(): could not find function "calc_CentralDose"

Instead, we have to explicitly load the package first so that R can actually find the function. The common way to do this is by using the function library(), which only requires the name of the package to be loaded.

library(Luminescence)
## Loading required package: magrittr
## Welcome to the R package Luminescence version 0.8.1 [Built: 2018-02-14 08:20:54 UTC]
## A motivated R-Team member: 'We are doing this not just for statistical reasons, there is real science behind it!'

Explicitly loading a package is always necessary after starting R, unless the package was installed in the System Library (which is not the default behaviour and another topic for another time).

Occassionally, you will also see the function require() used to load a library. While both functions essentially do the same thing, there are fine nuances to the differences between both functions that particularly favor the use of the library() function.