If you are reading this, you have successfully navigated the transition from the S226 Shiny R Tutorials to RStudio. Well done! RStudio is an integrated development environment (IDE) for R. By default it is set up with four ‘panes’:
Top left (this one) is the code pane where you write code
Bottom left is the R Console: this is where you can see what is going on with running the code in R. You won’t need to interact with this much, and may wish to make it smaller.
Top right has a series of tabs, the main (or only) one you will use is the Environment tab which is open by default and will contain any data objects you create.
Bottom right has another series of tabs. The main one you wll use is the Files tab which is open by default and shows all the files and folders on your system. It should be open in the Tutorial 5a folder.
You can re-size any of these panes to suit your needs.
This document is a ‘computational notebook’. That is a form of document that includes text like this, code and code output. It is a way that you can write, refine and run code, make notes and view output all in one document. It also makes it much easier to share your work with others. Computational notebooks are one form of what is called literature programming: making computer programs easier for people to read and interact with.
This notebook is written in a version of a simple formatting language called Markdown, and is tailored for R. Hence the file name for this file is Tutorial 5a.Rmd (.Rmd = R Markdown).
If you look at the top left of this document you will see two
buttons: Source and Visual. The Source editor
button shows the plain text version of the document: we don’t recommend
you look at that. The Visual editor button shows the formatted version,
and looks much more like the sort of word processor documents and web
pages you are used to. We recommend you work in the Visual editor.
This notebook is fully editable. You can type text like this, format headings, insert pictures, tables and links, just like in a standard word processor. Have a go here:
You can add a code chunk by clicking the green button with a
+C above . When
you do that, select R as the programming language and it will make a
chunk like this one:
sum(2,2,6)
## [1] 10
You can type code in the line beneath the {r} and run it
with the little green arrow to the right, or with the keyboard:
ctrl+Shift+Enter.
Add a code chunk below, type something simple into it (like
sum(2,2,6)) and run it to see the output.
You can (and should!) save your work frequently by clicking the standard ‘save’ icon in the coding pane. You can save your whole project by clicking the save icon in the main toolbar at the top of the RStudio window.
The rest of this tutorial runs through the coding that you did in the first part of the tutorial, so you can see how a notebook works.
You should be able to see the data folder in your files
pane (bottom right in the RStudio window). If you open that folder, you
will see the anchoveta.csv data file for this tutorial.
Although you can view the file, you still need to read it in by running
read.csv(). Do that in the code chunk below. Note that when
you add a chunk it is unnamed and you don’t have to name a chunk, but it
can be a good idea to do so. To name a chunk, simply include a space
after {r in the first line of the chunk and then write a
unique name.
Run the chunk below.
catch<-read.csv("data/anchoveta.csv")
Note that:
in the R Console (bottom left) the code that you have run appears, showing that it has run in R
in the Environment pane (top right) you now have a heading called
Data and the data object catch is present with
some basic details displayed.
View the structure of the data. See how the output from this chunk
prints below the chunk. You can also view some of the data structure
information by clicking the blue circle with a white arrow next to the
data object name catch in the Environment pane (top
right).
str(catch)
## 'data.frame': 69 obs. of 2 variables:
## $ year : int 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 ...
## $ catch: int 1100 7900 16100 37700 44400 66300 130000 346300 778600 2022000 ...
The code below creates three vectors, one each for years between 1950 and 2018 with a moderate, strong and very strong ONI. As El Niño events occur over the turn of the year, the years indicated below represent the year at the end of each episode. Run the code.
# Moderate ONI years
EN.m<-c(1952,1964,1969,1987,1995,2003,2010)
# Strong ONI years
EN.s<-c(1958,1966,1973,1988,1992)
# Very Strong ONI years
EN.vs<-c(1983,1998,2016)
Because these three data objects are just vectors they appear under a separate heading in the Environment pane (top right).
The chunk below uses the plot() function to plot
anchoveta catch against year. The plot will appear below the code
chunk.
plot(data=catch,catch~year,type="l")

The chunk below recreates the plot above, using the
abline() function to add vertical lines for the El Niño
years. If you amend the code in a chunk and re-run it the output will
update. Try and change the line width, colour or linetype and re-run the
code.
# Here we have increased the thickness of the catch data timeseries to make it stand out more
plot(data=catch,catch~year,type="l", lwd=1.5)
abline(v=EN.vs,col="black")
abline(v=EN.s,col="red")
abline(v=EN.m,col="blue")

In this tutorial you have continued to develop your data visualisation skills and made the transition to RStudio and learned about RMarkdown notebooks. As you would expect of a tool that is used by many of the world’s leading environmental and data scientists, RStudio is a complicated piece of software that you can do a lot with. But at the heart of it is the coding skills that you have been developing. You are now set up to write and run your own code in your own development environment.
One last thing you can do is to export this file to a variety of formats, including Microsoft Word. This is done with the ‘Knit’ button on the top bar of the coding pane: look for the icon with the ball of wool and a knitting needle. Clicking the button will create the default output: html (basically a webpage). If you click the small black arrow to the right of the Knit button you can see the other formats. Try ‘knitting’ this notebook to a word document - it will either open automatically in Word or ask you to download it to your system and open from there. See what the output looks like. You may want to use this when preparing the data skills sections of your TMA01.