Start a new project by clicking on the button in the top right corner
Then go to Section 1.3 to learn how to navigate RStudio.
The free account allows for 25 project hours/month, which may not be enough for this class. If you run into time limits or you are using this for another class you will have to upgrade to the Cloud Student plan which is $5/month.
Using the cloud is easier to initially setup, but having your own installation on your computer ensures that
you want to keep the program free forever
you will be able to put your files under version control
you always have access to your code even with unstable or no internet
The basis of programming is that we write down instructions for the computer to follow, and then we tell the computer to follow those instructions.
We write, or code, instructions in R because it is a common language that both the computer and we can understand (with practice).
We call the instructions commands and we tell the computer to follow the instructions by executing (also called running) those commands.
Learning how to code is like learning a new language. There are rules and exceptions, and it can be extremely precise and sometimes frustrating in how literal it is.
Writing clear commands can be like explaining how to make a PB&J sandwich to a child.
This class does not assume that you have any knowledge in programming, and will provide all the codes that you need. For best results you should be trying to understand this new language, and how to tell R to do what you want it to do.
The tendency may be to reach out to AI to help write code for you, but this does not help you learn and worse can provide code that is overly complicated and does not work as intended.
1.3 Navigating R Studio
We will be interacting with the programming language R only through R Studio. Not by itself. Typically are four panes, or windows, in R Studio. When you first start the program, you may only see three.
The major windows (or panes) of the RStudio environment:
The console (left side) is where you see the execution of commands. You can type commands directly into the console and press Enter to execute those commands.
R is an overgrown calculator
👉 Your Turn
In the console type the following code, then press Enter.
2+2
[1] 4
Once these commands are submitted (the code is run) they will be forgotten when you close the session. To save your commands you’ll want to write them in a script file.
Source
This pane is where you will write/view R and Quarto scripts. Some outputs (such as a dataset) will appear as a tab here.
👉 Your Turn
Click on File –> New File –> R Script to open an R script file.
You should now have four panes, with a new untitled1.R script file in the top left. When you write R code here you can save these commands, and rerun them later on demand.
👉 Your Turn
In the script file type 2+2 and then then press CTRL+Enter while your cursor is still on the same line.
2+2
[1] 4
You will see the command echoed down in the Console along with the answer. We will come back to using scripts in the next lesson.
Environment / History
The Environment / History pane (top-right) shows all the objects you’ve created in your current R session — for example, data frames, variables, or plots you’ve assigned.
The Environment tab lists everything that currently exists in memory. This could be data sets, model results and defined variables.
👉 Your Turn
In console run data(cars). This loads a built-in data set named cars into your environment. Click on the word cars in the top right, and notice it opens the data set in a spreadsheet like view in the Source pane.
The History tab keeps a record of all the commands you’ve run.
There are also some additional tabs in this area, like Connections, Build, Tutorial, and sometimes Git. We won’t cover those here, but RStudio has many additional tools and features available as you learn more.
Files / Plots / Packages / Help
The Files / Plots / Packages / Help pane is used to explore files, view plots, manage packages, and read help pages.
1.4 Customizing
Setting preferences in R Studio
AKA: Retain sanity while troubleshooting
Open RStudio and go to Tools → Global Options…
Uncheck “Restore .RData into workspace at startup.”
For “Save workspace to .RData on exit:” choose Never.
Click Apply, then OK to close the window.
This ensures that each time you restart R, you start with a clean environment instead of carrying forward objects from a previous session.
R is considered an Open Source software program. That means many (thousands) of people contribute to the software. They do this by writing commands (called functions) to make a particular analysis easier, or to make a graphic prettier.
When you download R, you get access to a lot of functions that we will use. However these other user-written packages add so much good stuff that it really is the backbone of the customizability and functionality that makes R so powerful of a language.
For example we will be creating graphics using functions like boxplot() and hist() that exist in base R. But we will quickly move on to creating graphics using functions contained in the ggplot2 package. We will be managing data using functions in dplyr and reading in Excel files using readxl. Installing packages will become your favorite past-time.
👉 Install ggplot2
Typing the following in the console to install the ggplot2 package.
install.packages("ggplot2")
When the download and install is complete, you should see a message similar to:
The downloaded binary packages are in
C:\Users\Robin\AppData\Local\Temp\Rtmpi8NAym\downloaded_packages
R is Case sensitive
R is case sensitive and spelling matters. If you get an error message like the following.
Warning in install.packages : package ‘ggplot’ is not available (for R version 3.5.1)
The correct package name is ggplot2, not ggplot.
Alternative Method of installing Packages
Additional packages can be installed from the ‘packages’ tab.
On the packages tab, click the ‘Install’ icon and start typing the name of the package you want in the text box.
At the bottom of the Install Packages window is a check box to ‘Install’ dependencies. This is ticked by default, which is usually what you want. Packages can (and do) make use of functionality built into other packages, so for the functionality contained in the package you are installing to work properly, there may be other packages which have to be installed with them. The ‘Install dependencies’ option makes sure that this happens.
Install all the things!
Now that you’re a package installing pro, go ahead and install the following packages that we will be using in the next few weeks.
👉 Your Turn
Install the list of packages to the left one at a time and wait for it to finish before entering the next command. Some of these contain functions that we will use, others contain data sets that we will be working with.
tidyverse
sjPlot
gtsummary
ggpubr
tinytex
here
RColorBrewer
viridisLite
openintro
palmerpenguins
nycflights13
Trust but Verify
Check to see if you have a package installed by looking in the packages tab. You can also type the command installed.packages() into the console and examine the output.
A note about the tidyverse.
“The tidyverse is an opinionated collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures.”
We will specifically be using the dplyr, ggplot2 and readxl packages that are part of, and thus installed with, the tidyverse. In these lessons we will load the specific package that we need (e.g. only dplyr or only ggplot2), but loading the tidyverse has the same effect of readying the necessary functions as loading each individual package. Learn more about the tidyverse.
1.6 Get oRganized!
It is good practice to keep a set of related data, analyses, and text self-contained in a single folder called the working directory. For example keeping all files related to one class in it’s own folder, separate from other classes. If you are taking another class using R at the same time as this one, it’s even MORE important to keep your files separate.
Important
THIS SHOULD NOT BE YOUR DOWNLOADS FOLDER
1.6.1 Names are important
Choose one of the four naming convention shown below for your class folder and stick with it. These are snake_case, camelCase, kebab-case or UPPER_SNAKE.
Common Naming Conventions
Create your folders
On your computer, in an easy to find place, create a new folder named math130 using your chosen naming convention.
Then create three subfolders: data, homework, notes.
1.6.2 Be the boss of your files
When you download a file, right click and “Save as” or “Save target as” and actively choose where to download this file.
Avoid these pain points
Do not left click on the links
Do not let files live in your downloads folder.
Do not open any files from your browser window after downloading.
👉 Your Turn
Right click [this link] to download and save this file into your homework folder. We will use this file in the next lesson.
1.7 Organization using R Projects
R projects are a great way of keeping all files for one project all together, and makes importing data much easier by using relative paths instead of absolute ones. Plus this ensures reproducibility by others (because no-one else stores their files at C:\users\rdonatello\myprojects\math130)
👉 Your Turn
Click on the R cube icon in the top right corner of RStudio and select Crete new project.
Choose Existing Directory and navigate to your Math 130 folder.
Check Open in new session. This allows you to have multiple Rstudio windows open for different projects and the files/data/objects don’t get cross-contaminated.
Click Create Project.
This will open a new R Studio window. Go ahead and close both R Studio windows for now (Don’t save Untitled1.R). We’ll see how to use R Projects next.
If you think about wanting to work on a report written in Microsoft Word, or a video that you want to edit, the common practice is to navigate to that file and double click to open that file in the appropriate program. We don’t tend to open the program first, and then try to open the file from within the program. Using R Projects is somewhat the same.
👉 Using R Projects
Navigate in your file explorer to the folder where you created your project.
Click on the R cube icon to open the whole project.
Using the files tab (bottom right) find and click to open the hello.qmd script file you downloaded earlier.
You can tell you are in an R Project by looking in the top right corner by the Environment pane and seeing if the R project cube has your class folder name
Example R Project
1.8 Final Tips
1.8.0.1 Saving and closing your work
Unless you’re returning to work in R Studio in a short while, you should make a habit to save all open tabs and completely shut down R studio when you are done working. This ensures your environment is cleared.
This is a good thing
1.8.0.2 Restart R
To restart R without shutting the entire window down, go to the file menu bar in the top,
Session –> Restart R and Clear Output
This is good to do when switching between projects/classes.