A Installing and Updating

Installing R and RStudio is usually straightforward. The sections below explain how and there is a helpful YouTube video here. If you have an older installation, you will want to update it to the newest versions of R and RStudio your computer can use.

A.1 Base R

A.1.1 Installing 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). You may also need to 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.

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

A.1.2 Updating R

Finally, you may also wish to update R itself. 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.

A.1.2.1 Windows

The easiest way to update R on Windows 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 available 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")

# Run the update function
installR::updateR()

A.1.2.2 Mac

For a Mac, you can use the updateR package. You'll need to install this from GitHub. You will be asked to type your system password (that you use to log into your computer) in the console pane. If relevant, it will ask you if you want to restore your packages for a new major version.

# install from github
devtools::install_github("AndreaCirilloAC/updateR")

# update your R version, you will need your system password
updateR::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 Installing RTools

If you are using Windows, after you install R, you should also install RTools; use the "recommended" version highlighted near the top of the list. RTools is used for installing and loading some packages. You can get started without installing RTools, but if you're having problems with installing and loading some packages, this should be the first thing you try.

RTools will require you to put it "on the PATH". The instructions for this can seem a bit vague - the easiest way to do it is to open RStudio, run the below code in the console:

write('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', file = "~/.Renviron", append = TRUE)

Once you've done that, restart R by clicking Session - Restart R and then run the below code in the console which should give you the path to your RTools installation:

Sys.which("make")

A.2.2 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.3 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, Lisa prefers two spaces instead of tabs for my code and likes 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 Packages

A.3.1 Installing Packages

This is done using install.packages(). This is like installing an app on your phone: you only have to do it once and the app will remain installed until you remove it. For instance, if you want to use PokemonGo on your phone, you install it once from the App Store or Play Store; you don't have to re-install it each time you want to use it. Once you launch the app, it will run in the background until you close it or restart your phone. Likewise, when you install a package, the package will be available (but not loaded) every time you open up R.

# type this in the console pane
install.packages("devtools")

Install the shinyintro package on your system. This package is the main package we will use throughout this book and is not on CRAN, so you will need to get it from github using the function devtools::install_github().

# type this in the console pane
devtools::install_github("debruine/shinyintro")

If you get a message that says something like package ‘shinyintro’ successfully unpacked and MD5 sums checked, the installation was successful. If you get an error and the package wasn't installed, check the troubleshooting section A.5.

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

A.3.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.

install.packages("tidyverse")

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 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. To generate PDF reports, you will additionally need to install tinytex (Xie, 2019) and run the following code:

# run this in the console
install.packages("tinytex")
tinytex::install_tinytex()

A.5 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 every explicitly load, but they're required beneath the surface for R to do things like knit your Markdown files etc.

A.5.1 Non-zero exit status

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 installr package also 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")

A.5.2 Cannot open file

You may get the following error after trying to install any packages at all:

Error in install packages : Cannot open file 'C:/.....': Permission denied

This usually indicates a permissions problem with writing to the default library (the folder that packages are kept in). Sometimes this means that you need to install R and RStudio as administrator or run it as administrator.

One other fix may be to change the library location using the following code (check in "C:/Program Files/R" for what version you should have instead of "R-3.5.2"):

# change the library path
.libPaths(c("C:/Program Files/R/R-3.5.2/library"))

If that works and you can install packages, set this library path permanently:

  1. Install the usethis package
  2. Run usethis::edit_r_profile() in the console; it will open up a blank file
  3. Paste into the file (your version of): .libPaths(c("C:/Program Files/R/R-3.5.2/library"))
  4. Save and close the file
  5. Restart R for changes to take effect

The code in your .Rprofile will now run every time you start up R.

As always, if you're having issues, please ask on Teams or come to office hours.