You are required to submit a report for this lab. Please follow the instructions below, complete “Lab1 report” and submit it on Canvas.
R is the based app to process R programming language, and RStudio
integrates with R to provide further functionality such as graphical
user interface (GUI). We need to install both R and
RStudio for this workshop.
Note: there are R and RStudio installed in the
computer lab. Please use the PCs in the lab if you cannot install.
We will install R first.
Windows users:
Mac users:
Next, we will install RStudio, the GUI of R.
Windows users:
Mac users:
It happens to me all the time that my course A files are mixed with course B’s. To make things easier, I highly recommend organizing your folders in the following way:
Remember that we have installed both R and RStudio earlier? But for this lab and for all future ones, we only need to run RStudio. This is because R is the hidden computing infrastructure while RStudio is the interface.
Run RStudio.
If it is the first time to run RStudio, it may ask you to “select the version of R to use”. Use the default option “64-bit version of R”, click “OK”. “Enable automated crash reporting” –> “Yes”
After launching RStudio, you will see the following interface (Figure
1.3).
Before creating a project, please make sure that you have already
created your course folder and the sub-folder “lab1” as
mentioned in Section 2: Good practice.
In RStudio menu –> File –> New Project –> Existing
Directory –> Browse & Navigate to the course folder, then the
sub-folder “lab1” –> Double clicks to go inside of
the folder “lab1” –> Open –> Create Project
Note: we will need to repeat this to create projects for future
labs and assignments.
A script is a sequence of instructions that can be executed by a computer or programming language (in our case, R). To create a new script, in RStudio menu –> New File –> R Script (see Figure 1.4)
Then, we also need to save the script to the “lab1” folder. File
–> Save –> Specify File Name –> Save (see Figure 1.5)
After creating and saving the new script, you should see something
similar to Figure 1.6.
Let’s try to coding now. First, let’s ask the machine to do math for us.
2+5+8
## [1] 15
Next, let’s asking the machine
to print some words for us. Type the following command and run it.
Hint: to run codes, please select lines and click “Run”
print("GES Geovisualization")
## [1] "GES Geovisualization"
Good Coding Practice:
tmap is a commonly used package in R for mapping. We install this package first. Type the command in your script and click “run”.
install.packages("tmap")
Then, we load the tmap package. Again, type –> select –> run
# load "tmap"
library(tmap)
The package tmap itself has a dataset on countries. Let’s load the data first, and create a world map based on their level of inequality.
Please run the command below on your local machine. What do you see under the Plot tab on bottom right? Click “Zoom”. Please do a screenshot of the map and paste it to your “Lab1 Report”.
# load the world data
data("World")
# create a map
tm_shape(World) +
tm_polygons("inequality")
Close the “Plot Zoom” window and go back to the script.
Next, we will create an interactive map to explore countries’ happy planet index (HPI). The Happy Planet Index is a measure of sustainable well-being in considering human well-being and environmental impact.
We will use a different color palette (Yellow & Green) for this map. Type the code to your script –> select –> run.
tmap_mode("view")
tm_shape(World) +
tm_polygons("HPI", palette = "YlGn")
Under the Plot tab, click “Zoom”. Please do a
screenshot of the map and paste it to your “Lab1
Report”.
Hover over the map and click any country, you should be able to see
their HPI index. Please report HPI indexes of any five countries to
“Lab1 Report”.
To close an R project, please go “File”–> “Close Project” – a pop window asking “Do you want to save these changes” –> “Yes”.