Search notes:

R function: library

Load (and attach) an add-on package.
The package to be loaded, by default, does not to be put in quotes (but see non standard evaluation)
library(ggplot2)

List of installed packages

Called without an argument returns a list of installed (not necessarily loaded) packages. Compare with installed.packages.

List of installed datasets

library(help="datasets") gets a list of datasets that are installed with the current R installation.

Non standard evalution

library(pkg_name) uses non standard evaluation (NSE) when evaluating the name of the package to to be loaded. This is why pkg_name needs not be quoted with apostrophes or single quotes.
While NSE safes two characters, it is a constant source of confusion when a package is attempted to be loaded whose name is stored in a variable. The following snippet causes the error Error in library(pkg) : there is no package called ‘pkg’. Execution halted
packages_to_load <- c('ggplot2', 'sqldf');

for (pkg in packages_to_load) {
   print(paste('Trying to load', pkg));
   library(pkg);
}
Github repository about-R, path: /functions/library/NSE/does-not-work.R
In order to specify the package to be loaded with a variable, the character.only argument needs to be set to TRUE:
packages_to_load <- c('ggplot2', 'sqldf');

for (pkg in packages_to_load) {
   print(paste('Trying to load', pkg));
   library(pkg, character.only = TRUE);
}
Github repository about-R, path: /functions/library/NSE/character.only.R
It is also possible to load a package from a variable with a combination of eval and bquote:
packages_to_load <- c('ggplot2', 'sqldf');

for (pkg in packages_to_load) {
   print(paste('Trying to load', pkg));
   eval(bquote(library(.(pkg))));
}

#
#  Show loaded packages
#
(.packages());
Github repository about-R, path: /functions/library/NSE/bquote.R
Another possibility is to use do.call and as.name:
packages_to_load <- c('ggplot2', 'sqldf');

for (pkg in packages_to_load) {
   print(paste('Trying to load', pkg));
   do.call('library', list(package = as.name(pkg)));
}
Github repository about-R, path: /functions/library/NSE/as.name.R

See also

require(), detach()
suppressPackageStartupMessages()
Index to (some) R functions

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1745520719, '3.138.112.77', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/R/functions/library(106): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78