R packages are collections of code that hold data and mainly functions used in R. There are three types of packages within R:

  1. Base packages - packages that come with R installation (base, compiler, datasets, graphics, grDevices, grid, methods, parallel, splines, stats, stats4, tcltk, tools and utils)
  2. Recommended packages (boot, class, cluster, codetools, foreign, KernSmooth, lattice, MASS, Matrix, mgcv, nlme, nnet, rpart, spatial, survival) -these packages are not loaded automatically, so you have to load them with function library(‘name_of_the_package’)
  3. User-contributed packages - all other packages written by the users.

Many packages include ”vignettes” which gives an overview of the package and sometimes includes examples.

If you want to see all installed packages you could use function installed.packages(). Under Priority column it is defined if the package is Base or Recommended.

Packages can be installed from anywhere, as long as you have access to the source or binary (compiled) folder containing the package. In order to keep some quality control over user-submitted packages (at least structural quality control) the official repository is hosted by CRAN and this tutorial will focus on installing from this location. GitHub is another popular location for packages and many of them host the raw and development code here and push occasional stable releases to CRAN. Some packages though are not available on CRAN and instead push releases to GitHub. Equally, you may find packages hosted elsewhere, and you could download a copmressed, usually *.zip, folder and install as a local packge. Ultimately, there is no guarantee that any of the code in these packages is actually correct, error free or even doing what you intend, and so, as with R itself, you use them under your own cogniscance.

#installed.packages() - there are a lot of packages, therefore it is mentioned here as a comment

1 Install packages

IMPORTANT - To install packages you need an internet connection

First we have to define CRAN downloading site as shown in new picture:

Tools global options!

Tools global options!

From R studio version v0.99.467 there is an option for secure downloads from CRAN as shown in picture:

Secure download with RStudio!

Secure download with RStudio!

You can install packages in two ways:

  1. Through RStudio packages pane

Fist click packages (STEP 1) on Packages pane on the right down part of RStudio screen as shown in the picture Package installation with RStudio. Thereafter click Install (Step 2).

Package installation with RStudio!

Package installation with RStudio!

Then new window will appear as shown in following picture:

Package installation with RStudio new window!

Package installation with RStudio new window!

Type in the name of the package you want to install and press Install button.

  1. Directly with code

You can install packages directly with code, first you define cran, then package to install and with library at the end you are uploading the package to R

options(repos=c(CRAN="https://ftp.heanet.ie/mirrors/cran.r-project.org/"))
install.packages('siar') # Be aware that R is case sensitive!
## 
## The downloaded binary packages are in
##  /var/folders/ql/6y0jh0mn2bn1t1gd9h0521940000gn/T//RtmpFXHZsT/downloaded_packages
library('siar')
## Loading required package: hdrcde
## Loading required package: mvtnorm
## hdrcde 3.1 loaded
## Loading required package: coda
## Loading required package: MASS
## Loading required package: bayesm
## Loading required package: mnormt
## Loading required package: spatstat
## Loading required package: nlme
## Loading required package: rpart
## 
## spatstat 1.51-0       (nickname: 'Poetic Licence') 
## For an introduction to spatstat, type 'beginner'
## 
## Attaching package: 'spatstat'
## The following object is masked from 'package:MASS':
## 
##     area
## 
## Attaching package: 'siar'
## The following object is masked from 'package:spatstat':
## 
##     convexhull

IMPORTANT - When you close you RStudio session, the packages will close too. Therefore, you need to load them next time you want ot use them.

1.1 Working with already installed packages

One thing that can be essential to bear in mind is that with the proliferation of packges, and the limtations of language, one can end up with two functions with identical names attached via two different pacakges. R will retain both, but you will get a warning along the lines of

"the following object is masked from package:purrr"
    map

In this case I loaded an installed library maps that has a function called map that has masked a similarly named function in the also attached package purrr. Masking means both functions still exist and are both available, but hierarchy means that the most recently loaded package will take precedence, and so if we simply call map, we will get the map function from the package maps. We can directly call the function we want within a package using the long-hand call of the form package::function (note the double colon), and it is best practice to always do this when using a non-base loaded package, which is often. The downside is that it reduces readability of code, at least casually, but increases robustness of the code and once you become more experienced it actually increases readability.

So, to call the functions from each package you would use:

maps::map()
purrr::maps()

1.1.1 Detach package

If you wish to detach some of already installed packages you can do it in two ways:

  1. Through RStudio packages pane

You could do that by removing checked box as shown on next picture:

R package check box !

R package check box !

If you uncheck this box (see picture: uncheck box) you will see in the Console window following function:

detach(“package:siar”, unload=TRUE)

R package uncheck box !

R package uncheck box !

However if you want to deinstall package bayesm that is required by package siar you will receive following notice:

package ‘bayesm’ is required by ‘siar’ so will not be detached

  1. Directly with code

You can do the same with function detach as follows:

detach("package:siar", unload = TRUE)

1.1.2 Remove package

  1. Through RStudio packages pane

If you want to remove some of the packages then you have to press X on the right end side of the name of the package as shown in red rectange on the picture.

Remove R package !

Remove R package !

  1. Directly with code

The same can be performed with remove.packages function as follows:

remove.packages("siar")
## Removing package from '/Library/Frameworks/R.framework/Versions/3.4/Resources/library'
## (as 'lib' is unspecified)

1.1.3 Update packages

  1. Through RStudio packages pane

If you want to update packages you have to click update and RStudio console will ask you about update for each new package that exist in your list of packages

Update R package !

Update R package !

  1. Directly with code

The same can be performed with update.packages function as follows:

update.packages() # it will ask you in the console window if you want to update packages with multichoice answer (yes/no/cancel)
## bayesm :
##  Version 3.0-2 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 3.1-0 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## bindrcpp :
##  Version 0.1 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 0.2 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## curl :
##  Version 2.6 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 2.7 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## DBI :
##  Version 0.6-1 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 0.7 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## dplyr :
##  Version 0.7.0 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 0.7.1 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## evaluate :
##  Version 0.10 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 0.10.1 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## foreign :
##  Version 0.8-68 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 0.8-69 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## glue :
##  Version 1.0.0 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 1.1.1 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## httpuv :
##  Version 1.3.3 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 1.3.5 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## ks :
##  Version 1.10.6 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 1.10.7 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## R6 :
##  Version 2.2.1 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 2.2.2 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## rmarkdown :
##  Version 1.5 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 1.6 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## sp :
##  Version 1.2-4 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 1.2-5 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?  
## tseries :
##  Version 0.10-41 installed in /Library/Frameworks/R.framework/Versions/3.4/Resources/library 
##  Version 0.10-42 available at https://ftp.heanet.ie/mirrors/cran.r-project.org
## Update (y/N/c)?

2 Help with package installation or removal

CRAN_help