PSYC40540: Getting started with RStudio

Jens Roeser, Andrew Mackenzie, Mark Andrews

Compiled Sep 13 2024

logo

1 Downloading and installing R and RStudio

Please make sure your computer / laptop that is compatible with R and RStudio (this will be the case for most of you). Both these software packages need to be installed. R is the backend language and RStudio is the graphical user interface. You will not need to open R. We will only work in RStudio. Ideally, avoid using machines that you don’t have full admin right for (e.g. university machines).

  • R can be downloaded here. Make sure you download the latest version compatible with your operating system.
  • RStudio can be download here. You need to scroll down and find your operating system.

Download installation files for both programmes and follow the installation instructions using default settings.

2 Installing packages (aka libraries)

Please make sure your version of RStudio and R are operating correctly. The best way to check that everything is operating is to install and then load a package. R is equipped with many built-in functions such as t.test or mean and this is what we call “base R function”. However, we can also add other functions for doing tasks that are not easily or at all possible with base R functions. These functions come from external packages. As such, we have to install and download these packages in RStudio.

Here are two ways you can install a package. We will install and load the package tidyverse which we will use a lot.

2.1 RStudio’s GUI

The first option is to use RStudio’s user interface. See screenshot.

  1. Open RStudio. Locate and click on the tab on the panel on the right that is called “Packages” in RStudio.
  2. Click on Install. In the Packages box, begin to type in tidyverse. You will see the package listed. Install this package. You should see on the console text similar to “package ‘tidyverse’ successfully unpacked and MD5 sums checked”.
  3. You must also load to package so that it is ready for use. Within the list of packages on the right-hand panel, scroll until you see tidyverse. Click on the check box next to tidyverse. This will then load the package.

2.2 Command line

Alternatively you can use RStudio’s command line to install and load the package tidyverse. Note, there is no need to install tidyverse again if you followed the steps above.

Make sure you locate the Console panel in RStudio (by default it is located on the left). In the console, type the following command and hit the Enter key to install tidyverse. Don’t forget the inverted commas.

install.packages("tidyverse")

This will run the installation for you. Again, you only need to install the package once.

After the installation is completed, use the library() function to load the package tidyverse. Again, type the command below into the console and hit Enter.

library(tidyverse)

If you see messages like these

## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

everything is fine even though it looks like something went wrong. If you encounter any errors or something doesn’t look right, we will be able to help you in our first workshop session.