RStudio is a program that helps you write and run R code more easily, especially for working with data, making graphs, and doing statistics.
You will have hopefully already downloaded R and RStudio. If not, follow the instructions on this page: R and RStudio setup
When we launch RStudio for the first time, we notice three main panels or quadrants:

Once we open a document, the interface will show four main panels:

This is your code editor, where you write, save, and run R scripts (.R), RMarkdown documents (.Rmd), or other file types.
This is the interactive R console, where code is executed immediately. You can:
This area lets you track what’s happening in your R session.
It has different tabs for different actions:
This panel contains various tools and outputs:
?function_name shows here).A good starting place for learning R is utilizing one of its most basic functions, which is that of a calculator. As mentioned above, there are four panels within RStudio, and to begin, let’s look at how this works in the R Console (bottom left panel).
You can add numbers using the plus sign +:
2 + 2
You can subtract numbers using a hyphen -:
10 - 4
You can divide numbers using a slash /:
20/4
You can multiply numbers using the star *:
10 * 10
A quick note on the R console
As you can see from the actions above, the R console can be used as a place to type very small calculations or actions. However, a big draw back of using the console is that you can’t save the calculations that you are creating, and there is no way to add comments to your code. This is why we use R script files, as they allow us to keep track of everything that we’re doing in R with sufficient documentation for others (and yourself!) to interpret.
There are two main types of files we can create in R studio to edit
our code (R script files .R and RMarkdown files
.Rmd)
The R script file (.R) is a plain text file that
contains R code only. Use it when you want to write and run code
line-by-line, such as for data cleaning, analysis, or function building.
It’s good for experimenting, scripting, and running code interactively.
It’s also good for sharing code with other researchers.
The RMarkdown file (.Rmd) has a mix of R code and
written explanations, using Markdown formatting. It can be used to
create dynamic, reproducible reports that can be knitted into HTML, PDF,
or Word documents.It’s great for homework, research reports, dashboards,
or combining narrative with code.
For the purposes of this program we’ll be using R scripts, but if you’re interested in learning more about RMarkdown, check out:
To create an R script file, select File > New File > R Script

Create your first R script file.
Let’s move to the R script file we have just created that is located in the top left panel. Let’s first try to re-run some of the mathematical calculations we ran above, but this time in the script:
2 + 2
10 - 4
20/4
10 * 10
Did it work?
Unless you have previous experience with R, or in you got quite
clever with using a search engine, you will have noticed that R won’t
simply run commands in a script when you press the
enter/return button. This is because the R script is
allowing you to freely type, edit, and adjust your code before it goes
ahead and deploys it (another advantage over using the console).
There are a few different ways that you can run code in your R script:
Run. If you click
on it, RStudio will run the line/chunk of code where your cursor is
located. If your cursor is above a line of code, it will run the line
below it, but if your cursor is below the code, it won’t run anything.
You can also highlight multiple lines of code and press
Run, and they will be run consecutively
Ctrl + Enter (Windows) / Command + RETURN
(Mac) - will run a single line/chunk of code that your cursor is onCtrl + Shift + Enter (Window) /
Command + Shift + Return (Mac) - will run all the code in
your R script, beginning at the top.Give these both a try with the calculations above, and start considering how you might use these as you move through your journey in learning R!
An object is anything you create and name in R. It can be a number, a dataset, a function, or even a plot. Objects take on content from everything to the right of the assignment operator.
An assignment operator is how you store a value in R. It’s like saying: “Let this name hold this value.” It assigns content from the objects/functions/arguments on its right to the object on its left.
a <- 5 # x is now an object that holds the value 5
b <- "Anna" # b is now an object that holds the character Anna
Note
You can overwrite a new value to the same object name. When you do this, the original value is replaced by the new value you assign to it.
name <- "Maria" # The "name" object now holds the value "Maria"
name <- "Anna" # The "name" object now holds the value "Anna", and no longer has the value "Maria".
Why overwriting is useful
As your analysis becomes more complicated, you often build your results step-by-step.
Instead of creating dozens of different object names, you can reuse the same object name to store updated versions of your data or results.
This keeps your environment clean and your code easier to read.
Objects can also be assigned several values. This is done by using
the c() command, which stands for concatenate, and where
the strings of values go into the brackets. You can think of this
function as “glueing” elements together into one group. It can be used
like this:
numbers <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sports <- c("Basketball", "Golf", "Soccer", "Tennis")
You’ll notice that the word strings, or strings of characters, are
surrounded by quotation marks "". This is because R, and
most coding languages, handle character strings uniquely, and require
quotations to surround words and phrases. We’ll cover this more in the
next section, but for now, just know that every time you want to use a
character string as a value in R, you need to use quotation marks.
Test yourself!
w, and assign it a value of
100.x, and assign it a value of
25.y, and assign it the value of
basketball.z, and assign it the value of
baseball.If this all worked, you will see in the top-right
Environment pane the four new values that you have
created.
Let’s keep playing:
+ to add w and
x./ to divide w by
x.w and x to give them new
values of your choosing.+ to add y and
z. Did it work? If not, what do you think might have
happened?Answers
1) w <- 100
2) x <- 25
3) y <- "basketball"
4) z <- "baseball"
5) w + x
6) w/x
7) Ex: w <- 500
x <- 9000
8) This should show an error like the following: "Error in y + z : non-numeric argument to binary operator". This is because R will only let you add numerical values together, and not character values. This is beyond the scope of this session, but for more information check out the session on [Data Types and Structures in R](Block4-3_Data-Types.html)
9) Ex: movies <- c("Pulp Fiction", "Lord of the Rings: The Fellowship of the Ring", "Highlander")
Functions are much like verbs in a language, as they convey some sort of action to be performed. These are often (though not always) actions to be performed on an argument. We’re going to learn more about functions in the next section, but here are a couple basic functions to show you how they work.
numbers <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
mean(numbers)
## [1] 5.5
In this example, the mean function is applied to the
numbers object (which in this case is the
argument), and gives the mean for the series of numbers.
For the sake of this session we’re not going to spend too much time on
functions, but they will be discussed in more detail next session as we
start getting deeper into the weeds of R.
Literate coding is a framework that provides a human-language explanation of how a script works so that people can accurately interpret and reuse the script. If you look at the script that you’ve currently got, it’s quite messy and difficult to interpret! This isn’t a huge issue because this is only a practice script, but it’s good to start developing good practices right at the start, so as you continue on your scripting journey, these principles become second nature. There are two primary ways we can start to support literate programming: commented code and object naming.
You may have noticed that in some of the example code blocks below,
there is a # used occasionally before text blocks. This
# is a very valuable tool in all coding languages, and
tells the coding language to ignore everything that follows it.
# Create an object called "numbers" that is carries the values 1-10
numbers <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# Calculate the mean of the object "numbers"
mean(numbers)
## [1] 5.5
As you can see in the example above, you can use the #
to write descriptions of what each chunk of code is doing, so that when
others look at your code, or if you look at code that you wrote in the
past, you can easily understand what each chunk is doing. This is an
integral part of literate coding and reproducibility, and something that
is practiced across all coding languages and domains.
Looking again at the script that you’ve created, there are a lot of
objects with single letters as names (w, x,
y, z). While using letters as object names
isn’t a problem when running small tests in practice scripts, it can get
very confusing when you start working on bigger projects and have
multiple objects to sift through. A much better approach, and one that
is similar to file naming that we discussed in a previous session, is to
give your objects short but meaningful names so that you and others can
easily make sense of the objects that you’ve created.
Much like file naming, the following parameters apply:
Now that we’ve established some best practices in literate programming, go through the script that you have current created, and utilizing commented code and object naming, try to make it easily interpretable and reusuable for others!
You can save your script by selecting file > save or
by clicking the flopping disk icon in the top navigation pane.
Name your script my-first-script.R.