A Installing and Updating

Installing R and RStudio is usually straightforward. The sections below explain how and there is a helpful YouTube video here.

From time-to-time, updated version of R, RStudio, and the packages you use (e.g., ggplot2) will become available. Remember that each of these are separate, so they each have a different process and come with different considerations. We recommend updating to the latest version of all three at the start of each academic year.

A.1 Installing Base R

Install base R. Choose the download link for your operating system (Linux, Mac OS X, or Windows).

If you have a Mac, install the latest release from the newest R-x.x.x.pkg link (or a legacy version if you have an older operating system). After you install R, you should also install XQuartz to be able to use some visualisation packages.

If you are installing the Windows version, choose the "base" subdirectory and click on the download link at the top of the page. After you install R, you should also install RTools; use the "recommended" version highlighted near the top of the list.

If you are using Linux, choose your specific operating system and follow the installation instructions.

A.1.1 R Online

You may need to access R and RStudio online if you use a tablet or Chromebook that can't install R.

Students in the School of Psychology and Neuroscience at the University of Glasgow can access Glasgow Psychology RStudio with their GUID and password.

RStudio Cloud is a free online service that allows access to R and RStudio.

A.1.2 Updating R

The key thing to be aware of is that when you update R, if you just download the latest version from the website, you will lose all your packages. The easiest way to update R and not cause yourself a huge headache is to use the installr package. When you use the updateR() function, a series of dialogue boxes will appear. These should be fairly self-explanatory but there is a full step-by-step guide for how to use installr, the important bit is to select "Yes" when it asked if you would like to copy your packages from the older version of R.

# Install the installr package
install.packages("installr")

# Load installr
library(installr)

# Run the update function
updateR()

A.2 Installing RStudio

Go to rstudio.com and download the RStudio Desktop (Open Source License) version for your operating system under the list titled Installers for Supported Platforms.

A.2.1 Updating RStudio

Typically, updates to RStudio won't affect your code, instead they add in new features, like spell-check or upgrades to what RStudio can do. There's usually very little downside to updating RStudio and it's easy to do.

Click Help > Check for updates; if an update is available, it will prompt you to download it and you can install it as usual.

A.2.2 RStudio Settings

There are a few settings you should fix immediately after updating RStudio. Go to Global Options... under the Tools menu (⌘,), and in the General tab, uncheck the box that says Restore .RData into workspace at startup. If you keep things around in your workspace, things will get messy, and unexpected things will happen. You should always start with a clear workspace. This also means that you never want to save your workspace when you exit, so set this to Never. The only thing you want to save are your scripts.

You may also want to change the appearance of your code. Different fonts and themes can sometimes help with visual difficulties or dyslexia.

RStudio General and Appearance settings

Figure A.1: RStudio General and Appearance settings

You may also want to change the settings in the Code tab. For example, some prefer two spaces instead of tabs for indenting and like to be able to see the whitespace characters. But these are all a matter of personal preference.

RStudio Code settings

Figure A.2: RStudio Code settings

A.3 Installing LaTeX

You can install the LaTeX typesetting system to produce PDF reports from RStudio. Without this additional installation, you will be able to produce reports in HTML but not PDF. This course will not require you to make PDFs. To generate PDF reports, you will additionally need to install tinytex (Xie, 2022) and run the following code:

tinytex::install_tinytex()

A.4 Installing Packages

Add-on packages are not distributed with base R, but have to be downloaded and installed from an archive, in the same way that you would, for instance, download and install PokemonGo on your smartphone.

The main repository where packages reside is called CRAN, the Comprehensive R Archive Network. A package has to pass strict tests devised by the R core team to be allowed to be part of the CRAN archive. You can install from the CRAN archive with the install.packages() function.

install.packages("tidyverse")

Never install a package from inside a script. Only do this from the console pane.

A.4.1 Development Packages

Many R packages are not yet on CRAN because they are still in development. Increasingly, datasets and code for papers are available as packages you can download from github. You'll need to install the devtools package to be able to install packages from github.

# install devtools if you get
# Error in loadNamespace(name) : there is no package called ‘devtools’
# install.packages("devtools")
devtools::install_github("psyteachr/reprores-v3")

If you use a development package in a script, it is good practice to include a comment with the website where you downloaded the package. Other people who use your script are unlikely to have that package or know where to find it.

A.4.2 Updating Packages

Package developers will occasionally release updates to their packages. This is typically to add in new functions to the package, or to fix or amend existing functions. Be aware that some package updates may cause your previous code to stop working. This does not tend to happen with minor updates to packages, but occasionally with major updates, you can have serious issues if the developer has made fundamental changes to how the code works. For this reason, we recommend updating all your packages once at the beginning of each academic year (or semester) -- don't do it before an assessment or deadline just in case!

To update an individual package, the easiest way is to use the install.packages() function, as this always installs the most recent version of the package.

To update multiple packages, or indeed all packages, RStudio provides helpful tools. Click Tools > Check for Package Updates. A dialogue box will appear and you can select the packages you wish to update. Be aware that if you select all packages, this may take some time and you will be unable to use R whilst the process completes.

A.4.3 Troubleshooting

Occasionally, you might have a few problem packages that seemingly refuse to update, for me, rlang and vctrs cause me no end of trouble. These aren't packages that you will likely ever explicitly load, but they're required beneath the surface for RStudio to do things like knit your Markdown files.

If you try to update a package and get an error message that says something like Warning in install.packages : installation of package ‘vctrs’ had non-zero exit status or perhaps Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : namespace 'rlang' 0.4.9 is being loaded, but >= 0.4.10 is required one solution I have found is to manually uninstall the package, restart R, and then install the package new, rather than trying to update an existing version. The rpkg("installr") package has a useful function for uninstalling packages.

# Load installr
library(installr)

# Uninstall the problem package
uninstall.packages("package_name")

# Then restart R using session - restart R

# Then install the package fresh
install.packages("package")