A Installing R and packages | Computational Historical Thinking (2024)

A.1 What is CRAN?

R has very good support on the three main platforms: Linux, MacOS, and Windows. Both R the language and the thousands of published packages which extend its functionality for the most part work across the three platforms. In addition, installing packages in R can be considerably simpler than in other languages. Much of the ease of installing R its add-on packages is thanks to the CRAN infrastructure.

R and the published packages are available at CRAN, the “Comprehensive R Archive Network.” The CRAN maintainers rigorously screen new packages, then test all accepted packages continually. This does not mean that there are no bugs in published R packages, but it does mean that the quality of software published at CRAN is generally pretty high.

CRAN is a collection of mirrors, that is, servers that contain the exact same content. The canonical URL for CRAN is https://cran.r-project.org/, but you can also access a CRAN mirror at Indiana University, for example, at https://ftp.ussg.iu.edu/CRAN/. The R Project maintains a current list of mirrors. When installing packages at the start of an R session, R will ask you which mirror you want to use. Unless you have a compelling reason to pick a different one, you should select the first option, “Cloud”: https://cloud.r-project.org/. That option will pick a server which is geographically close to you.

A.2 Installing R on MacOS

CRAN compiles R for the Mac. To install R, go to CRAN and click on the link to download R for Mac. Download the installer for the current version of R, for example, R-3.6.2.pkg. Install this package like you would any other Mac software. The installer will install R, plus various other software such as a GUI application for R.

You can test that you have R installed properly by opening the Terminal program on your Mac and running the following command.

R --version

If you get a message that prints the current version of R, then you have successfully installed R.

In general, you should use the version of R that CRAN compiled for the Mac unless you have a specific reason not to do so. One of the chief advantages of this version is that you can install R extension packages which have been pre-compiled by CRAN for the Mac. This can make it significantly easier to install R packages.

A.2.1 Installing R on a Mac via Homebrew

You can however install R via Homebrew. Homebrew is a package manager for the Mac which lets you install and update software from the command line. You might want to install R this way if you plan on compiling your own R packages, or if you are using Homebrew for installing all the other software in your development environment.

First follow the instructions to install Homebrew on its website. Then in your terminal, run the following command. This will probably involve installing a lot of additional software, such as compilers.

brew install R

Once you’ve gone down the road of compiling your own software, you will also need to install various system libraries to compile certain R packages. For instance, if you need the R package xml2, you will also needthe libxml2 library. Generally speaking, the documentation for the individual packages will explain what system libraries they might need and how to install then.

A.3 Installing R on Windows

CRAN maintains a version of R for Windows,, along with helpful documentation on how to install it. Follow the instructons on the CRAN download page for Windows.

The version of R that you install from CRAN for Windows will be able to install binary (i.e., precompiled) versions of R extension packages. This is considerably easier than trying to compile those packages yourself. However, CRAN also maintains a set of Rtools for Windows which provide the software that you will need to compile packages for yourself. If you wish to build packages from source, then install the version of Rtools that corresponds to the version of R you have installed.

A.4 Installing R on Ubuntu

Being able to install R on a Linux system is useful in several ways. Of course, if your primary computer runs a Linux distribution, then you can use R on it like you would on Mac or Windows. But you can also install R on a Linux server, or run R on a Linux virtual machine hosted on a different operating system. These instructions give you the commands for installing R on either the server or desktop version of Ubuntu 18.04.x, the latest long-term support (LTS) version of a popular Linux distribution.23

Software on Ubuntu is generally installed via the apt package manager. A version of R is available in the Ubuntu repositories, but it it almost certainly not the most recent versions, especially in LTS versions of Ubuntu. Therefore it is best to add a CRAN-maintained Ubuntu repository to the list of software sources that apt checks, then install R.

First, run this command (and all following commands) in the Ubuntu terminal. This command gets the security key to verify the software.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

Then add the CRAN maintained Ubuntu repository.

sudo add-apt-repository 'deb https://cran.rstudio.com/bin/linux/ubuntu xenial/'

Finally install R. In addition to installing R, these commands will install the Ubuntu packages that you need to be able to compile R extension packages for yourself.

sudo apt updatesudo apt install r-base r-base-dev

If you can run the following command and get the correct version of R, then you have installed R correctly.

R --version

Many R packages will require common Ubuntu packages for development before they can be installed. Installing these will set up your system with the correct libraries for common R packages.

sudo apt install build-essential libcurl4-openssl-dev libssl-dev libxml2-dev

A.5 About R packages and libraries

CRAN hosts many packages for R. These packages extend R’s functionality by providing additional functions. And R packages can also include data files, which is an easy way to distribute example data or data for learning that is not too large.24

These extensions to R are called packages. In other programming languages they might be called libraries. But in R, the term library is used for the directories where R packages are installed. It is important to remember that R can load packages from multiple libraries at a time. You can see which libraries your R session knows about with the function .libPaths().

.libPaths()
#> [1] "/Users/lmullen/R/library"#> [2] "/usr/local/Cellar/r/3.6.2/lib/R/library"

The second library in that list of directories is the library that comes bundled with R. It includes packages like the stats and utils packages which provide basic functionality to R. This library is kept in a system directory and is available to all users of R on a system.

The first library in that list is a personal package library. It is kept inside the user’s home directory, in this case, the user lmullen. This library contains packages which the user has installed, and they are only accessible to that user. When you load a package with library() or require(), those functions look for a package in the first directory returned by .libPaths(), then the second and so on, stopping when it finds the first match. This ensures that versions of packages that you install are loaded in preference to system libraries. When installing packages for the first time, you will probably be prompted to create a personal package library.25

A.6 Installing packages from CRAN

Any package on CRAN can be installed using the install.packages() function.

install.packages("tokenizers")

You can install multiple packages by passing a vector of package names to the function, for example, install.packages(c("dplyr", "stringr")).

That function will install the requested packages, along with any of their non-optional dependencies. Unless you have configured your system differently, the packages will be installed in a personal package library somewhere in your home directory.

Once you have installed packages, you can check for updates by running the update.packages() command. This will check CRAN for newer versions of the packages that you have already installed, and it will prompt you to install any that have been updated.

If you have a version of R that supports it—namely Windows and the CRAN compiled R for some versions of MacOS—then install.packages() will install a binary version of packages if one is available.26 When packages are installed from source, R creates a version which can be installed and used directly. Many R packages contain code written in a lower-level language, such as C++. When installed from source, those packages must be compiled into a binary. If possible, install.packages() will install a pre-compiled binary from R.

If a binary version of a package is not available for your version of R, then install.packages() will compile the package for you. This will require you to have a compiler and possibly system libraries installed. The instructions on installing R above will show you how to get those dependencies for your operating system. Certain packages may require you to install system libraries. For instance the sf package, used for geospatial analysis, requires the GDAL/OGR libraries. These can be installed on a Mac via Homebrew and on Ubuntu with the apt package manager. Until these libraries are installed, the package will fail to compile.

Each package that CRAN publishes has a page that lists information about the package. This information includes a README which usually explains how to get started with the package, a manual that has documentation for each function and dataset in a package, often vignettes which explain some aspect of using the package, along with the name of the package maintainer, where bug reports can be filed and so on. For example, here is the CRAN page for the dplyr package.

A Installing R and packages | Computational Historical Thinking (1)

Figure 1.2: CRAN listing for the dplyr package.

The CRAN page also lets us know how a package can be installed. In this case we can see that dplyr requires compilation. But we can also see that pre-compiled binaries are available for Windows and Mac. If external system libraries were required, they would be listed under SystemRequirements, with some indication about how those requirements could be met on different platforms.

A.7 Installing packages from GitHub

Not all R packages are published to CRAN. Sometimes they are not suitable for CRAN, or are unfinished. Other times you may wish to install the development version of a package, even if a stable version is available on CRAN.

Many R developers keep a version control repository of their packages on GitHub. For example, the historydata package has a repository on GitHub. (These repositories are generally listed on a published package’s CRAN page.) We could install historydata from CRAN if we wished, but the development version has additional datasets which have not yet been published.

We can install historydata from GitHub using the remotes package. First install that package if necessary. Then using the name of the user and the GitHub repository, in this case ropensci/historydata, we can install the development version of historydata.

install.packages("remotes")remotes::install_github("ropensci/historydata")

Note that once you have installed a package from GitHub, R will not keep it updated with update.packages() unless the package is published to CRAN with a more recent version number than the package you installed from GitHub. You can, however, reinstall the package from GitHub if there are changes that you want to pick up.

A.8 Configuring R

R is both a programming langauge and an interactive environment for data analysis. R users spend more of their time using the interactive environment than users of most other languages. R thus tries to save your variables, plots, and the like (called an environment) when you close your session. Then it tries to reload that environment when you create a new session. This seems desirable for new users, but it breeds very bad habits. It teaches you to just use the variables in your environment, rather than ensuring that you write scripts which can reproduce your results from raw data to finished project. I strongly recommend that you disable this default behavior.

If you are using RStudio, you can go to Tools > Global Options > General and make sure that you set your options as in the figure below.

A Installing R and packages | Computational Historical Thinking (2)

Figure 1.4: To make sure that R does not save your environment between sessions, change these options in RStudio.

If you are using R at the command line (which you will probably want to do at some point), you can start your session with these flags:

R --no-save --no-restore-data

You may choose to set a alias in Bash or your preferred shell to ensure that you always start R this way.

alias R="R --no-save --no-restore-data --quiet"
A Installing R and packages | Computational Historical Thinking (2024)

FAQs

How do you install packages in R? ›

In R, you can easily install and load additional packages provided by other users. or click Tools > Install packages. Write the package name in the dialog, then click install. Once you install the package, you need to load it so that it becomes available to use.

Do I need to install R packages every time? ›

We only need to install a package once on our computer. However, to use the package, we need to load the library every time we start a new R/RStudio environment. You can think of this as installing a bulb versus turning on the light.

Why is my R package not installing? ›

Make sure that the package is available through CRAN or another repository, that you're spelling the name of the package correctly, and that it's available for the version of R you are running.

How to install all R packages at once? ›

You can install multiple packages by passing a vector of package names to the function, for example, install. packages(c("dplyr", "stringr")) . That function will install the requested packages, along with any of their non-optional dependencies.

How to install package in R manually? ›

Go into R, click on Packages (at the top of the R console), then click on "Install package(s) from local zip files", then find the zip file with arm from wherever you just saved it. Do the same thing to install each of the other packages you want to install.

How to install packages in R with command? ›

Steps to Install a Package in R
  1. Step 1: Launch R. To start, launch R on your computer. ...
  2. Step 2: Type the command to install the required package. Use the following template to install your package: ...
  3. Step 3: Select a Mirror for the installation. ...
  4. Step 4: Start using the package installed.

Where should I install my R packages? ›

R packages are installed in a directory called library. The R function . libPaths() can be used to get the path to the library.

Why do we need R packages? ›

They save time — you don't need to think about the best way to organise a project, you can just follow a template. Standardised conventions lead to standardised tools — if you buy into R's package conventions, you get many tools for free.

What is an efficient way to install R packages? ›

The most efficient way to install the R packages is by installing multiple packages at a time using. For installing multiple packages we need to use install. packages( ) function again but this time we can pass the packages to be installed as a vector or a list with each package separated by comma(,).

How do I know if R package is installed? ›

To see what packages are installed, use the installed. packages() command. This will return a matrix with a row for each package that has been installed.

Do you need to reinstall packages in R? ›

Every time you upgrade R by a major or minor version (e.g. 3.6 to 4.0, or 4.0 to 4.1) a new user package library is created. All the packages that you previously used need to be downloaded and reinstalled - a bit of a pain but at least you get all the latest versions!

How do I update my R installation? ›

Upgrading your version of R

If you decide to upgrade your version of R, simply choose a CRAN mirror in the download page and follow the procedure you used to initially download and install R. The installation will automatically install the new version and make it available within RStudio.

How often do you need to load a package in R? ›

While you only need to install a package once, you need to tell R to use that package any time you start a new R session. In the SSCC, you will find that there are many packages already installed for you. You can install or update packages yourself - these will automatically be installed in a folder on your U:/ drive.

How to install packages in R with all dependencies? ›

For example, in RStudio, the most popular IDE for R, we need to complete the following steps:
  1. Click Tools → Install Packages.
  2. Select Repository (CRAN) in the Install from: slot.
  3. Type the package name (or several package names, separated with a white space or comma)
  4. Leave Install dependencies ticked as by default.
Apr 13, 2022

Can I install multiple versions of R? ›

Installing Multiple Versions of R

To install multiple versions of open-source R side-by-side you will typically need to compile and build R from source. See this article for more. RStudio Workbench will scan a specific set of directories to find all versions of R currently installed on the system.

How to install packages in R offline? ›

To install these packages offline, you can download a compressed file that matches your operating system from ; copy the file to your offline machine. In some cases you'll have to download the dependencies packages as well; if this is required, it will be noted under your package link.

How to install ggplot2 in RStudio? ›

Installing ggplot2

The ggplot2 package can be easily installed using the R function install. packages() . The above code will automatically download the ggplot2 package, from the CRAN (Comprehensive R Archive Network) repository, and install it.

How to use pip to install packages? ›

In this example, you run pip with the install command followed by the name of the package that you want to install. The pip command looks for the package in PyPI, resolves its dependencies, and installs everything in your current Python environment to ensure that requests will work.

How to install data table package in R? ›

How to install data. table library and to use data. table instead of data. frame in R
  1. i = (equivalent to where clause in SQL) you put the row condition out here.
  2. j = (equivalent to select clause in SQL) you put the column conditions out here.
Jun 15, 2022

Top Articles
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 6252

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.