Section A.1 Exploring RStudio
You can use R without using RStudio, but RStudio is an app that makes it easier to work with R. RStudio is what we call an IDE, an integrated development environment. It is a fancy way of saying that it is a cool interface designed to write programming code. Every time you open up RStudio, you are in fact starting an R session. RStudio automatically runs R in the background.

When you first open RStudio, you will see (as in the image above) that there are three main windows. The bigger one to your left is the console. If you read the text in the console you will see that RStudio is indeed opening R, and you can see what version of R you are running. Since R is constantly being updated, the version you installed is likely more recent than the one we used at time of writing.
Subsection A.1.1 Opening up the script pane
The view in RStudio is structured so that you have four open windows in a regular session. However, when you first open, you might be starting with only three. To open the script pane (the one missing), click in the File drop-down Menu, select New File, then R Script.

You will now see the four window areas in display. In each of these areas you can shift between different views and panels. You can also use your mouse to re-size the different windows if that is convenient.

Subsection A.1.2 The four panes of RStudio
The purposes of the four panes in the figure above are the following:
-
Script and data view: where you type your code that tells
Rwhat you want to do. These are essentially instructions that you type and save as a script, so that you can return to it later to remember what you did and to share it with others so that they can reproduce what you did. -
Environment and history view:
-
Environment tab — gives you the names of all the (data) objects that you have defined during your current
Rsession, including number of observations and rows in those objects. We learn more about objects later. -
History tab — shows you a history of all the code you have previously evaluated in the main console. One of the key advantages of doing data analysis this way, with code versus with a point and click interface like Excel or ArcGIS, is that you are producing a written record of every step you take in the analysis. First time around, it will take you time to write these instructions; it may be slower than pointing and clicking. And unlike with pointing and clicking you need to know the "words" and "grammar" of this language.
-
-
Main console: this is considered
R’s heart, and it is whereRevaluates the codes that you run. You can type your code directly in the console, but for the sake of good habits, type them in the script and data view so you can save a record of them. Only type and run code from here if you want to debug or do some quick analysis. -
File Directory, Plots, Packages, Help:
-
Files tab — allows you to see the files in the folder that is currently set as your working directory.
-
Plots tab — you will see any data visualizations that you produce here. You have not produced any yet, so it is empty now.
-
Packages tab — you will see the packages that are currently available to install. We will explain what these are soon, but know that they are an essential feature when working with
R. -
Help tab — you can access further information on the various packages.
-
Subsubsection A.1.2.1 Interacting with the four panes
In the previous section, you opened up the ’script’ pane. We now write some code in it, and see what happens.
To do this, go to your open version of
RStudio, and type in the script pane the following:
print("Hello world!")
When you have typed this, you will have typed your first bit of code. Yet nothing is happening? That is because you also have to run the code.
You can do this by highlighting the code you wish to run, and clicking on ’run’ in the top right-hand corner. When you ’run’ the code, it will print the text ’Hello World!’ in the bottom pane, which is the console. That means you have written and executed your first line of code.
In the rest of the appendix, we will be unpacking how this all works, and getting more familiar and comfortable with using
RStudio.
To recap: the script is where you write your programming code. A script is nothing but a text file with some code on it. Unlike other programs for data analysis you may have used in the past (Excel, SPSS), you need to interact with R by means of writing down instructions and asking R to evaluate those instructions. R is an interpreted programming language: you write instructions (code) that the R engine has to interpret in order to do something. And all the instructions we write can and should be saved in a script, so that you can return later to what you did.
As mentioned earlier, one of the key advantages of doing spatial data analysis with code versus with a point-and-click interface like ArcGIS or MapInfo (or even QGIS) is that you are producing a written record of every step you take in the analysis. First time around it may be slower than pointing and clicking, however over time you can re-run and re-use your code and will save lots of time (as well as build transparency and reproducibility into your analysis). Once you have written your instructions and saved them in a file, you will be able to share this file with others and run your every time you want in a matter of seconds. This creates a reproducible record of your analysis: something that your collaborators or someone else anywhere (including your future self, the one that will have forgotten how to do the stuff) could run and get the same results than you did at some point earlier. This makes science more transparent, and transparency brings with it many advantages. For example, it makes your research more trustworthy. Don’t underestimate how critical this is. Reproducibility is becoming a key criteria to assess good quality research. You can read up on reproducibility and its importance in [220] or [221] for more detail.
