4 + 2Week 10 - Presentation of the R interface and how to create reproducible data
CHC9012 | National Taiwan Normal University
Overview
This week, we will have an introduction to the R language and the RStudio interface, which will be useful to analyze our data.
Get to know R:
- Basics: What is R, and what is RStudio?
- Simple coding: What are the most important functions in R?
A brief summary of the essential elements:
[To complete later]
Please complete the following tasks before our next meeting:
- Download the script of this week
- Annotate each line of code (use your own words, these annotations are for your own reference!)
- Upload the annotated script on Moodle
🎒 Part 1: Setup & Installation
So far, we have been talking about something called “R/RStudio”. People familiar with this program know what this is about. However, it can be quite confusing when we have never heard of it. We are actually referring to two distinct, yet connected programs:
The Engine: R
The main program where all the mathematical magic and deep computations happen.
The Dashboard: RStudio
A user-friendly interface that makes it incredibly easy to interact with R.
Maybe a metaphor can help to understand the difference between R and RStudio. As you are engaging yourself in this tutorial, I assume that you have a computer. You can create folders, open Word or Excel documents, write in them, and save them in the folders you created.
Now it is time to reflect on what you are doing. You have done these tasks by using your mouse or your touchpad and clicking on icons (or figures) on your screen. What you may not know is that when doing so, you have actually run many hidden lines of code!
It is your computer that ran them in programs able to perform computations. Your role has been to trigger these programs by using the user-friendly interface installed in your computer.
This is the exact same thing with R and RStudio. R will perform the computations that you ask for. And you will ask R to do stuff by clicking on icons and writing commands using the user-friendly interface of RStudio. And this is why you need to download two programs!
Step 1: Download and install R
Go to the RStudio website by clicking here. You will see something like this:

Next, choose your CRAN (The Comprehensive R Archive Network). Let’s choose the one in Taiwan. You will need to scroll down the page to find the link.

Choose the files to download according to your computer’s operating system. The most common are macOS (for Apple computers) or Windows.

On the next page, choose the subdirectory. For our purposes, we only need the “base” subdirectory.

We finally got to the last page! Click on the first link to download the files, as shown below. Wait until the file is downloaded, open it, and follow the standard instructions to install R on your computer.

Step 2: Download and install RStudio
Now that R is installed, it is time to do the same with RStudio. Go to the RStudio website by clicking here. You will see something like this:

Scroll down to look for the download links. Again, choose the right file for your operating system. Just click the link, let it download, open the file, and follow the instructions!

Look for the program called “RStudio” on your computer. Once you find it, just open it up, and you can move on to the next section!
Recall that R is the program where computations are done, and RStudio is the user-friendly interface. Even though we needed to download both, you actually only ever need to open RStudio when you want to use R! RStudio will talk to R in the background for you.
💻 Part 2: Working in RStudio
Now I assume that R and RStudio are correctly installed. When you start RStudio, you will see a lot of things on your screen—many blocks, many menus. It can be quite overwhelming. No worries! This is exactly what this section is about.
General Overview: Blocks, Blocks, Blocks!
When you open RStudio for the very first time, you should see something similar to the picture below. (Note: If yours looks slightly different, that is completely normal!)

The best way to apprehend RStudio is to see the interface as a collection of blocks, each serving a specific purpose. There are four main blocks. I give them unofficial names to make them easier to remember:
- 💻 The Computer Block
- 🗂️ The Variable Block
- 📝 The Script Block
- ⚙️ The Console Block

(You may not be able to see the ‘Script block’ on your computer just yet. Don’t worry, we will open it soon!)
💻 The Computer Block (Bottom-Right)
Click on the Files button in this block. You will see a list of files and folders from your computer. This interface allows you to communicate and navigate directly with your hard drive, right from inside RStudio!
Take a second to play with it: Click on the folders. Try to navigate to your Documents or Desktop. There is no mystery here—it works exactly like navigating folders on your computer normally does!
🗂️ The Variable Block (Top-Right)
The most important tab here is the Environment. This is where variables you create while coding are temporarily stored. Let’s demonstrate this. In the ‘Console block’ (bottom-left), type this code and press Enter:
And indeed, the output shows 6. Now, let’s play a game. You want to obtain the number 8, but you have to use 4 + 2. You could write:
4 + 2 + 2But what if the math gets incredibly complex? This is where variables come in. Try this code:
a <- 4 + 2Nothing appears in the console anymore. But look at your Variable block!

You have stored the calculation inside a variable called a. You can call it anytime simply by typing a in the console, or even add to it by typing a + 2!
⚙️ The Console Block (Bottom-Left)
You just had a glimpse of it. The console block is the engine room where the code is actually run. When you type commands here and press Enter, R executes them immediately.
(There are also Terminal and Background Jobs tabs here, but we can safely ignore them for this tutorial).
📝 The Script Block (Top-Left)
The Script block is your digital notepad. If you type code in the Console, it disappears when you close RStudio. If you want to save hundreds of lines of code, you need a Script!
Let’s open one. Click “New File”, and then select “R Script”.

Type the following lines into your new Script block:
4 + 2
a <- 4 + 2
a
a + 2Select all the text with your mouse, and click ‘Run’.

Now you can go to File > Save and save this script to your computer. The next time you open it, all your code will still be there!
🎨 Exercise: Make it your own!
Your four blocks are like construction toys. You can change their size by dragging the borders, or even change the background color to a dark theme!
Path: Tools > Global Options... > Appearance > Editor theme

You can do amazing things with R: draw maps, run complex statistics, and manipulate massive datasets. But it can feel overwhelming to write all that code from scratch.
Good news! R is a collaborative, open-source community. Nobody writes all their code from scratch! We rely on “Packages” or “Libraries” built by other people.
| Concept | Definition |
|---|---|
| Line of code | A single, simple instruction (e.g., 4 + 2). |
| Function | A pre-packaged cluster of code that performs a specific task. |
| Package / Library | A downloadable folder containing hundreds of useful functions! |
There are thousands of packages available for free, often coming with their own fun hex-logos.

How to install and use Libraries
You only need to install a package once, but you must load it every time you open RStudio.
In your Console, type this to install ggplot2 (a famous plotting package):
install.packages("ggplot2")Once it finishes installing, you load it into your current session by typing:
library(ggplot2)Imagine you want to cook a complex dinner.
install.packages()is like going to the bookstore to buy a recipe book. You bring it home and put it on your shelf.library()is like taking the book off the shelf and opening it on your kitchen counter. You have to open it every time you want to use those specific recipes!
1. The Working Directory
Before doing anything with data, you must tell R exactly which folder on your computer to look in. This is your Working Directory.
Use the Computer Block to navigate to your project folder. Click the Gear Icon, and select “Save as Working Directory”.

3. Loading Data
Let’s import a .csv dataset (like an Excel file). We use the read.csv() function and assign it to a variable called data.
data <- read.csv("GACTT_RESULTS_ANONYMIZED_v2.csv", header=TRUE)Alternatively, you can use the RStudio interface! In your Variable block, click “Import Dataset” > “From Text (base)”, select your file, and click Import.

4. Transforming and Summarizing Data
Once loaded, you can inspect your data using str(data) to see its structure, or summary(data) to get a quick mathematical overview.
Often, survey data comes in as plain text when it should be categorized into groups (Factors). We fix this using as.factor():
data$What.is.your.age. <- as.factor(data$What.is.your.age.)(The $ symbol tells R to look inside the dataset for a specific column!)
If we want to count how many people fit into different groups, we can use the powerful dplyr package:
library(dplyr)
data3 <- data2 %>%
group_by(What.is.your.age., How.many.cups.of.coffee.do.you.typically.drink.per.day.) %>%
summarize(Count = n())
5. Plotting the Data
With our data summarized, we can use the built-in plot() function to visualize it:
plot(x = data3$What.is.your.age., y = data3$Count)(If your groups are out of alphabetical order, you can use the factor(..., levels=c(...)) command to manually sort them before plotting!)
Please complete the following tasks before our next meeting:
- Download the script of this week
- Annotate each line of code (use your own words, these annotations are for your own reference!)
- Upload the annotated script on Moodle
2. Comments
Use the
#symbol to write notes to yourself. R will completely ignore anything written after a hashtag!