x <- 8
z <- x + y
y <- 52 What is Code
2.1 Welcome to RStudio!
RStudio is the integrated development environment (IDE) we will be using to interact with R. RStudio is a very powerful environment that has lots of capabilities. We will learn many of these as we go, so right now we are going to focus on the most important aspects!
2.2 RStudio Layout
When you open RStudio on your computer, you should get something that looks like the image below. You should see three panes, two on the right hand side and one on the left. Let’s learn what these do!
On the left, you will find the Console pane. The Console is where you will see your code execute! In the top right, you can find the Environment and History tabs. These tabs show any objects you’ve created and a history of the code you ran. In the bottom right, you will find the Files, Help, and Packages tabs. These tabs allow you to access files on your computer, install and load packages, and access documentation (help) pages for functions you might be trying to use.
2.3 What is code?
Code is a set of instructions that tells a computer what to do and programming languages provide a structured way for humans to communicate these instructions to computers. Similar to a recipe giving step-by-step directions to bake a cake, code gives step-by-step directions for a computer to follow. Because computers require very precise instructions, programming languages have specific rules about how code must be written (kind of like constraining that your recipe can only be written with metric units).
One of the main advantages of code is that it allows tasks to be performed automatically and repeatedly. Instead of manually performing calculations or organizing information, we can write code that tells the computer how to carry out those steps for us. Once written, code can be run again and again, making it easier to work with large amounts of data and ensuring that the same process is applied consistently each time it is run.
2.3.1 What makes R different?
R is a statistical programming language. Unlike more general-purpose languages, R is optimized for working with data and doing statistics. R was created by Ross Ihaka and Robert Gentleman in 1993 (hence “R”) and was formally released by the R Core Group in 1997 (a group of 20-ish volunteers who are the only people who can change the base, built-in functionality of R). If you want to build an independent, standalone graphical interface, or run a web server, R is probably not the ideal language to use (you might want C / Python or PHP / Python, respectively). If you want to vacuum up a bunch of data, fit several regression models, and then compare the models, R is a great option and will be faster than working in a more general-purpose language like C or base Python.
One thing to know about R is that it is open-source. This means that no company owns R (like there is for SAS or Matlab) and that developers cannot charge for the use of their R software. This does not mean that all of your code needs to be public (you can keep your code private), but it is important to be a good open-source citizen by sharing your code publicly when possible (in STAT 3820 you will learn about GitHub), contributing to public projects and packages, creating your own packages, and using R for ethical and respectful projects.
R
If you would like to learn more about the history of R, here is an excellent article written by Roger Peng.
2.4 Running Code (in the Console)
When you open RStudio, the console pane should display information related to your R session. You should see something like the message displayed below. This message tells you two things (1) R was successfully detected on your computer, and (2) the version of R with which you are working.
R version 4.5.2 (2025-10-31) -- "[Not] Part in a Rumble"
Copyright (C) 2025 The R Foundation for Statistical Computing
Platform: aarch64-apple-darwin20
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
If you don’t see information related to your R session when you open RStudio or RStudio tells you it doesn’t detect R on your laptop you either forgot to install R or installed it in an unexpected location.
2.4.1 The Console Tab
It is possible to run code directly in the console! This code can be simple arithmetic like 2 + 2 or 3 * 6 or 6 - 4. The output of each line is showed below (e.g, [1] 4). Next week we will learn what the [1] means with the output, so for now we will focus on the number that is output.

Run these same three lines of code in your console.
2.4.2 Creating Objects
It seems a bit silly to use R as a calculator when it can do so much more for us! A key aspect of working in R is the idea of storing values into objects. When storing a value into an object, your code will look something like this: x <- 5
Believe it or not, there is a lot going on in this simple line of code. So, let’s break it down.
xis the name of the object we are creating5is the value we want to store insidex<-is an operator that binds the namexto the value5
The <- works similar to an = sign, but we are specifically using an <- because we believe it makes the action of assignment more explicit.
Create two objects in your console, one named x which has the value 3 stored in it and one named y which has the value 4 stored in it.
You should have noticed when you created x and y that the values are not printed, they are just stored. If you want to view the value, you would need to type x to see its contents.
Type x and y in the console to preview the contents of these objects!
2.4.3 Environment tab
Now that you’ve created two objects, there are some interesting things to notice in the upper right corner. The Environment tab displays any objects which are defined in memory. Currently, both objects you’ve created should be labeled “Values”, but later on we will start to see objects labeled “Data” and “Functions”.

2.4.4 The History Tab (Top Right)
Another useful tab in this pane is the History tab, which shows you a running list of every command you’ve ever run. At this point you should have run (at least) five lines of code, all of which should be displayed in the history.

2.4.5 Updating Objects
Currently, the values of x and y are 3 and 4 respectively, but we might need to update their values. If we want to change the values of x or y, we simply need to assign them new values.
Code tracing is a great technique for understanding what happens to an object at each line of code. Use the table below to trace the values of x, y, and z for each of the three lines of code above.
| Line | x |
y |
z |
|---|---|---|---|
| Line 1 | 4 | Does not exist | |
| Line 2 | |||
| Line 3 |
2.5 Saving Your Code
This process of updating objects in the console seems a bit tedious and error prone. If we made a mistake we can’t modify our previous code, so we will need to write another line of code. Moreover, it would be a bit tedious to get a history of all the code we ran. Enter R scripts!
2.5.1 Scripts
An R script is a document that can store your code! Specifically, an R script is a plain text file whose entire purpose is to store R commands. Instead of typing code line-by-line into the console, we can write our code in a script, save it, and run it whenever we need.
Working in an R script has several advantages. First, it allows us to easily edit and fix mistakes without rewriting code from scratch. Second, scripts provide a complete record of the steps we took to perform an analysis, making our work easier to understand, reproduce, and share with others (or with our future selves!). Finally, scripts help us organize our code, encouraging clearer structure, comments, and good programming habits as our projects grow.
Let’s create our first R script!
2.5.1.1 Creating an R Script
In the upper left hand corner, you should see an image of a white sheet with a green plus sign. If you click this button you should see something like the image below:

If you click on the R Script option, a new file should appear and your RStudio should look something like this:

Create an R Script in your RStudio!
2.5.2 The Editor Pane
Now that we have a document to edit, we can see the fourth pane in RStudio—the editor pane. There are many different files we can create in RStudio. The logo in the top left of the document indicates the type of file we have open. An R script file has a blue circle with the letter R.
Let’s start typing in the script, so we can explore some of the other buttons available to us.
2.5.2.1 Running Code
Copy the code below into your R script, so we can explore the various ways to run code.
a <- 1
b <- 4
c <- a + b
fname <- "Jane"
lname <- "Hollander"
Now that we have code in the script we can see that line numbers are provided on the left. This is a handy way to see where in the code the errors occur. There are a few ways to run code:
place your cursor on the line you want to run and click the run button
place your cursor on the line you want to run and hit Command + Enter (or Control + Enter)
Either of these options will run the line of code that your cursor is on and then hop to the next line (if there is one).
Run each of the five lines of code in your R script. Try both methods of running code and see which one you prefer!
2.5.3 Saving Your R Script
You’ll notice that currently your script is labeled “Untitled1” that is because we haven’t saved the file anywhere. If we were to try and close our file before saving RStudio would tell us that our document had unsaved changes. Let’s save our document so we have access to it later on!
You can save your file a few different ways. First, you can always use Command + S (or Control + S) to save a file. Second, you can click on the blue floppy disk immediately below your file name.
Either of these options should open a popup menu for you to select where you want the script to be saved. Navigate to your stat-1810 folder that lives in your Documents folder. Change the file name to week-1.R.

2.5.4 Files tab
Now that you’ve saved your script, it should appear in the Files tab in the lower right corner. By default, the Files tab starts at your computer’s working directory (the hard drive). If we wanted to get to a specific file, we could click through the path to get to that file.
To get to the week-1.R file I needed to click through two folders:
Documentsstat-1810

2.5.5 Writing Code for People
While scripts are a great way to store our code, scripts often don’t make it easy for people to understand the processes enacted in the code. Yet, in almost every data science setting someone else will be reading your code. So, it is important to learn how to write readable code. As you become more proficient at programming, you will realize quickly that writing readable code is more challenging than writing working code but it is possibly just as important as getting a working solution.
2.5.5.1 Code Comments
One way to make code easier to understand is the use of code comments. Since an R script can only contain R code, a code comment is your way of interweaving a written description into a script. Fundamentally, a code comment starts after a #. The # symbol indicates to R that this line of text should not be executed as code.
You can use code comments to describe the actions taken by the code. Notice how each code comment below describes what is happening in the subsequent lines. Stylistically, it is important to have a space between the # and the text so the comment is easier to read.
# Creating variables x and y
x <- 8
y <- 5
# Creating z to depend on current values of x and y
z <- x + yYou can also use code comments to comment out a line of code, so it is not executed. Note that we are still using a space after the # to make the line of code (being commented out) easier to read.
# Creating variables x and y
x <- 8
y <- 5
# Creating z to depend on current values of x and y
# z <- x + y# to use?
Technically, you could use as many # as you want when making a code comment (e.g., #### Creating variables x and y). However, it is not recommended to use more than one # for consistency and readability.
2.6 Exiting RStudio
Now that we’ve accomplished what we set out to do, we can close RStudio. When you try and close RStudio, you should get a message like the one below. You might wonder what this message means. Let us tell you!
When you exit an R session, you’re faced with the question of whether or not to save your workspace. The workspace is an image of all the objects you created while you were working in R into a file called .RData. If you save your workspace, when you reopen RStudio that workspace will be loaded and all the objects you created previously will be available to you. This might sound like something you might want, but actually can cause some major headaches.

Loading a saved workspace turns your R script from a program, where everything happens based on a series of known steps, to something more akin to a cardboard box full of assorted pages and pictures that you don’t remember where they came from and why you made them. This has two major issues. First, this action can clutter your workspace with lots of objects you don’t need. Second, the versions of the variables stored in your workspaces might be different from what you want them to be (or think they are) and will cause issues in your code.
Fundamentally, you should never need to save your workspace because your scripts should contain all the code you need to create the objects that you want to store. So, let’s walk you through how to never save your workspace.
Click “Cancel” in the pop-up window above (with the message about saving the workspace image). Now, at the top file menu for RStudio, click on Tools. In the dropdown menu that appears, click on “Global Options”.

In the menu that appears, you want to uncheck the box next to “Restore .RData into workspace at startup.” Next, you should select “Never” from the dropdown menu next to “Save workspace to .RData on exit” (there are three options). Once you’ve changed these options, click “OK” and the popup window should close.

Now, it is safe to exit RStudio!