[<-- Home](https://verbingnouns.github.io/AdventuresInR/)

# Setting up RStudio

Congratulations! You have downloaded [R](https://www.stats.bris.ac.uk/R/) and [RStudio](https://posit.co/download/rstudio-desktop/#download).

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook (".Rmd"). If you are reading this on the workshop website, you are looking at the HTML (website) version. Make sure you have the `.Rmd` file as well, which you can download by right-clicking [here](Setting_Up.Rmd) and saving as `Setting_Up.Rmd`. You can also click the "Code" dropdown menu in the upper right corner of this page and select "Download .Rmd"

The HTML (web) version can serve as an easy-to-read but static copy. You can only run the code if you open the `.Rmd` file using RStudio, since it is the code that built the static version.

When you execute code within the .Rmd notebook while you're using RStudio, the results appear beneath the code. This will be handy for working through the material outside of class and when you work on your own code in the future.

## File structure

Before we get started in RStudio, make sure you have a folder dedicated to the workshop. 


**Quick aside:** *Just so we're all on the same page... computers are designed to act like old-school filing systems. They're all structured like a filing cabinet where each drawer has folders, and inside those folders can be smaller folders, and your papers can be filed in any of these locations. On a computer, there are folders which can contain files or more folders, which can in turn can contain files or even more folders. To identify where a file is located in the computer's structure, every file has a "file path", which is a list of folders one would need to pass through to access the file.*


You will be running something called an RStudio **project** (`.Rproj`), which will house all of the files and code we work with during the workshop. Here's an example image of what your file structure should look like. I've called the folder "AdventuresInR", and it contains three other folders: "data", "scripts", and "figures".

![](images/windows-files.png)

![](images/mac-files.png)

Save the `.Rmd` file directly into `AdventuresInR` and open it in RStudio.

# Preparing RStudio

If you are currently reading this in **RStudio**, try executing this chunk by clicking the *Run* button within the chunk (the green triangle in the top right corner of the grey cell below) or by placing your cursor inside it and pressing `Cmd+Shift+Enter` (Mac) or `Ctrl+Shift+Enter` (Windows). If you are currently reading this using a **web browser**, you can hide or show the code that produces various outputs by clicking the *Hide*/*Code* button on the top right of the code "cell", but you cannot execute the code. It may help to have this notebook open in RStudio and a browser window at the same time, so you can navigate between them.

Execute this code now, then save this file (`Cmd+s` on Mac, `CTRL+s` on Windows). Once you have done that, a graph will appear below in RStudio.

```{r, eval=TRUE}
plot(cars)
```

This graph was generated using some data that comes pre-loaded with the basic R software. However, to use R you will need to:

1. **install**
2. **load**, and
3. **run**

packages that do not automatically come with the basic R software. These packages contain important additions to the basic software that allow you to perform sophisticated analyses and produce clear, beautiful visualisations, among many other things. Some packages can take a while to install, depending on the speed of your internet connection. Therefore, you must install these packages before the first day of the workshop. Please contact one of us well in advance of the workshop if you encounter any problems with installation.

## 1. Installing

**Installing** a package only needs to be done once (per computer). This is because installation is equivalent of downloading a file from the internet and storing it on your hard drive (e.g., Local Disk or C: drive on Windows or /Downloads/ folder on Mac). Once a package has been downloaded, it exists on your computer and does not need to be re-downloaded later.

First, make sure you are connected to the internet. Install the following packages now, by executing the code below as you did in the previous cell.

```{r, eval=FALSE}
install.packages("tidyverse")
install.packages("knitr")
install.packages("cowplot")
install.packages("ordinal")
install.packages("broom")
install.packages("emmeans")
install.packages("Hmisc")
install.packages("corrplot")
```

If you get an error or if nothing seems to be happening, you may need to respond to the program like below (type `yes` and hit `Enter`).
```
Would you like to use a personal library instead? (yes/No/cancel) yes

```

While this code is running, a red square (the 'stop' symbol) will appear in the upper right corner of the code cell. When it has finished running, the green triangle (the 'run' symbol) will reappear. Wait for the code to finish before moving on. This may take a while. Make sure to give yourself plenty of time to run this code, and if you have any trouble, contact one of the workshop organisers.

## 2. Loading

Loading a package is an entirely different action from installing a package. By telling R to load a package, you are instructing it to look for the file you downloaded during installation and sort of "wake it up" while you have R open. When you close R, the package will go back to sleep and you'll have to reload it again. (You will not have to download and install it again, although you may need to download updates periodically.)

Let's load the packaged called *ggplot2* right now by executing the following code.

```{r}
library(ggplot2)
```

If this code was successful, it will appear as though nothing happened. To show you what **an error** looks like, run the following code.

```{r, eval=FALSE}
library(Ggplot2)
```

Hopefully, this code produced text that reads:

    Error in library(Ggplot2) : there is no package called ‘Ggplot2’
    
Uh oh! Looks like *capitalisation* is important in R. That will be something to remember for later.

## 3. Running

By this point, you have **installed** and **loaded** the three packages you will need for the first day of the workshop. Before you're done setting up RStudio, though, you should make sure that the packages are **running** properly. Let's produce that first graph again, but this time using code only available when 'ggplot2' is installed and loaded.

```{r, eval=TRUE}
ggplot(cars, aes(x=speed, y=dist)) + geom_point(size=3, shape=1) + theme_bw()
```

If everything has gone to plan, you should now see a second graph that looks very much like the first graph. Don't worry about how we made it yet! That will be something we explain during the workshop. Right now, we just want to make sure that you are ready to hit the ground running on the first day.

If you run into any trouble before the workshop, *please* email one of the instructors in advance. We can help you troubleshoot and get your tools working before the workshop. During the workshop, we will have limited ability to help, which could slow you down or interfere with the workshop.

To be safe, you may also want to create an account at Posit Cloud: [https://posit.co/products/cloud/cloud/](https://posit.co/products/cloud/cloud/)

If you do this in advance, and something goes wrong on the day of the workshop, you will not need to lose any time with troubleshooting!

> **See you soon!**