Skip to main content

Current Chapter

Current chapter – Understanding R packages


R packages allow you to package and distribute R code and data for re-use by others.

To use the content of an R package, such as a data set, you need to ‘load’ the package in your R session.

RStudio includes a variety of pre-installed R packages. Some examples of these pre-installed packages are:

  • load data - DBI is the standard for communication between R and relational database management systems
  • manipulate data - dplyr has shortcuts for sub setting, summarising, rearranging and joining data sets together
  • visualise data - ggplot2 lets you use grammar and graphics to build layered customisable plots
  • model data - caret has tools for training regression and classification models
  • report results - with shiny you can explore data and share findings with non-programmers
  • save time series and financial data - zoo provides a format for saving time series objects in R

Viewing pre-installed R packages

To view the list of pre-installed R packages available in RStudio, either:

1. Click on the Packages tab.

A list of R packages and a description for each will be displayed.

understanding packages

2. Run the following command in the Console window.

 installed.packages()

A list of R packages will be displayed.

installed packages

More information on these packages is available on the Posit website.


Loading R Packages (RStudio desktop)

To make the contents or an installed R package available for use within your current R session, you will need to ‘load’ the package.

To load an R package, select the tick box alongside the R package you want to load.

Loading Pakages

Now that you have loaded the R package, you can use its functions in your current R session.


Loading R packages (RStudio Server)

Once a package has been made available within the SDE, you need to install the package into your RStudio Server session.

You can use the following script to do this (this script is also available in the “wrangler-example-notebooks" repository).

packages = c(“package1”,”package2”)
install.packages(packages, contriburl = “

file:///mnt/resources/rstudio/miniCranRepo/src/contrib”, dependencies = TRUE)

When you run this script, packages will be installed only for you. They will persist between RStudio Server sessions but will not persist when the RStudio Server cluster is restarted.

The way RStudio is set up means it will default to checking the online CRAN repository before looking at the SDE miniCRAN repository. You may get the warning below but this can be ignored as the packages will then start to install using miniCRAN.

Install package script

 


Last edited: 12 February 2025 1:43 pm