R packages

R language is divided in packages. Some are part of the standard R system and mostly others are loaded on demand.

List packages installed in the system

> library() # Show which packages are installed in the system.

See which packages are currently loaded (that appear in the search path)
> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"

E.g: Load codetools package:
> library(codetools)
> search()
[1] ".GlobalEnv" "package:codetools" "package:stats"
[4] "package:graphics" "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods" "Autoloads"
[10] "package:base"

Help about a package

Information on a package:
E.g: package base

> library(help = base)
> help(package = base)

Where to get a list of R packages

Standard packages which come with R installation:
base, compiler, datasets, grDevices, graphics, grid, methods, parallel, splines, stats, stats4, tcltk, tools, utils.

Which add-on packages exist for R (some recommended packages and repositories)?:

Contributed packages in CRAN:

List available packages from R interpreter:
> available.packages()

Show packages loaded by default:
> getOption("defaultPackages")
[1] "datasets" "utils" "grDevices" "graphics" "stats" "methods"

Install CRAN packages

> install.packages() # It asks for a mirror and then asks for a package to install.

E.g: I tell it to install gdata package:

Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages() :
'lib = "/usr/local/lib/R/site-library"' is not writable
Would you like to use a personal library instead? (y/n)

GCC compiler is needed for the source to compile.

gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c roman2int.c -o roman2int.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c setTCPNoDelay.c -o setTCPNoDelay.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-z,relro -o gtools.so roman2int.o setTCPNoDelay.o -L/usr/lib/R/lib -lR
installing to /home/vicente/R/x86_64-pc-linux-gnu-library/3.2/gtools/libs

or I could install a specific package directly:

> install.packages("gdata")
Installing package into ‘/home/vicente/R/x86_64-pc-linux-gnu-library/3.2’

Load a package:
> library(gdata)

> search()
[1] ".GlobalEnv" "package:gdata" "package:stats"
[4] "package:graphics" "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods" "Autoloads"
[10] "package:base"

You can unload the loaded package by:

> detach("package:gdata")


> detach("package:gdata", unload = TRUE)
(where unload = TRUE is needed only for packages with a namespace, see ?unload).

Find help for a funtion within the package:
> ?gdata::read.xls


> help(package=gdata, read.xls)


List status of installed packages:
> packageStatus()

Number of installed packages:

ok upgrade unavailable
/home/vicente/R/x86_64-pc-linux-gnu-library/3.2 2 0 0
/usr/local/lib/R/site-library 0 0 0
/usr/lib/R/site-library 0 0 0
/usr/lib/R/library 24 5 0

Number of available packages (each package counted only once):

installed not installed
https://ftp.cixug.es/CRAN/src/contrib 17 8366

Update packages:
> update.packages()
boot :
Version 1.3-17 installed in /usr/lib/R/library
Version 1.3-18 available at https://ftp.cixug.es/CRAN
Update (y/N/c)?


From a running R process they can be removed by

> remove.packages(c("pkg1", "pkg2"),
lib = file.path("path", "to", "library"))

> remove.packages("gdata")