CHC9012 | National Taiwan Normal University
Get to know R:
The main program where all the mathematical magic and deep computations happen.
A user-friendly interface that makes it incredibly easy to interact with R.
🧠 Think about it
With a computer, you can create folders, open Word or Excel documents, write in them, and save them in the folders you created.
You used your mouse or your touchpad and clicking on icons (or figures) on your screen to do so.
It is your computer that ran your behavior in programs able to perform computations. Your role: Trigger these programs by using the user-friendly interface installed in your computer.
R = perform the computations that you ask for.
RStudio = The user-friendly interface for 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.

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!
💡 Wait, why didn’t we open R?
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.
🎯 Objectives for this section
When you open RStudio for the very first time, you should see something similar to the picture below.

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:

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!
Environment tab: Where variables you create while coding are temporarily stored.
In the ‘Console block’ (bottom-left), type this code and press Enter:
Introduction to variables:
Nothing 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!
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.
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
| 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! |
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):
🍳 The Cookbook Metaphor
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!
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”.
Use the # symbol to write notes to yourself. R will completely ignore anything written after a hashtag!
Data to use
You need to download this file and place it in the folder for this class in the Documents folder of your computer.
Let’s import a .csv dataset (like an Excel file). We use the read.csv() function and assign it to a variable called data.
Alternatively, you can use the RStudio interface! In your Variable block, click “Import Dataset” > “From Text (base)”, select your file, and click Import.

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():
(The $ symbol tells R to look inside the dataset for a specific column!)
With our data summarized, we can use the built-in plot() function to visualize it:
To start, click “New File” > “R Markdown…”

RStudio will prompt you for a Title and Author. Once created, you will see a document with a YAML Header at the top between dashed lines ---. This header controls the settings for your final document.

In an R Markdown file, anything you type normally becomes standard text. To actually write R code, you must insert a Code Chunk. Click the “Insert” button at the top of the script and select “R”. A grey box will appear.

You can run these individual chunks using the green Play Button in the corner of the grey box.

When your document is complete, click the Knit button at the top of the interface. R will automatically run all your code chunks, generate the graphs, format your text beautifully, and produce a polished final document you can share with the world!
📝 Assignment for next week
Please complete the following tasks before our next meeting: