2 Lab 2: Workflow basics
2.1 Learning Outcomes
- Work with Projects in RStudio
- Create a new Project
- Adjust Project Options
- Use R scripts
- Create an R script
- Run code from an R script
- Copy code from a tutorial into an R script
- Use packages in RStudio
- Locate the Packages tab in RStudio
- Search or browse the list of installed packages
- Install a new package
- Load and attach a package using the Packages tab
- Load and attach a package using the
library()
function - Unload and detach a package
- Work from an online R tutorial
- Copy blocks of code
- Paste them into an R script
- Modify code to meet project needs
- Export images to use in other documents
2.2 Work with Projects in RStudio
RStudio Projects help you keep all the files associated with a given project together in the same place. This could include things like data, code, and figures. For more information about Projects, see:
- Using RStudio Projects – Posit Support
- R for Data Science (2e) - 6 Workflow: scripts and projects (hadley.nz)
2.2.1 Create a new project
Let’s make a project for you to use while you complete this lab.
Click File > New Project…, then follow the steps shown below:
Create Project: New Directory
Project type: New Project
-
Create new project:
Directory name: enter the name of your project, e.g. Lab 2
Create project as a subdirectory of: ~
Create Project
You should be able to find the Project Folder on this computer if you need to, for example to copy data files into, or to back it up to a flash drive.
Practice opening the File Explorer in Windows and navigating to the Project Folder. If you can’t find it, look in the Files tab in RStudio, click the “More” icon (a blue gear) and select “Show Folder in New Window”. When the window opens in the File Explorer, practice navigating from there to other important folders on the computer (e.g. “Downloads”) and back again. Here is some helpful documentation on using the File Explorer in Windows:
2.2.2 Adjust Project Options
Change your project options following the instructions on the Project Options page
2.4 Install the packages
Install the two packages you will need for this assignment.
- tidyverse
- palmerpenguins
To install, go to the Packages tab in RStudio, click the Install button, and type the names of the packages. Click Install and wait for the messages to stop scrolling in the console tab.
An alternate way to install the packages is to run the code in the box below. You can copy the code by clicking the copy button in the upper right corner of the code box. Paste it into your console and press Enter to run it.
install.packages(c("tidyverse", "palmerpenguins"))
2.5 Create a script
Until now, you have run commands by typing them into the console. One of the benefits of R over programs such as Excel, however, is the ability to save your commands in the form of written code. Enter the script! A script is simply a text file containing commands for R to execute.
- File > New File > R Script or click the icon with a green plus sign over a white rectangle; or use the keyboard shortcut Ctrl + Shift + N.
Note: Learn more about Keyboard shortcuts in Windows here and in RStudio here. - The script will open in the new Source pane above the console
Note: documents which contain code are often called “source” documents. - Give the new file a name. Click the save button (a blue floppy disk) or go to File > Save or type Ctrl+S. In the dialog window that pops up, give the file a name like “my script”. It doesn’t matter what you name it.
Warning: if you start typing a file name that is the same as your project name, the computer may try to autocomplete the file name, for example as “Lab 2.Rproj”. Do not save the file with this name or very strange things will happen. - Click Save and the new script will now be named, and will show a “.R” extension after the name in the tab in the source pane.
2.5.1 Use a script
Scripts are documents where you can write and save code for use later.
For the rest of the course, you will usually write your code in a script, not directly into the console.
To run the code in a script, put your cursor on the line you want to run and click the Run button (white rectangle with a right arrow) or type the keyboard shortcut Ctrl + Enter
RStudio will copy the code to the Console, run it, and move the cursor to the next line of the R script. You can run multiple lines of code in a row by repeatedly clicking run or typing Ctrl + Enter, but be careful to watch for errors or other output in the console as you do this.
2.6 R packages
In R, users share code with each other in the form of **packages**, each of which is a bundle of code, data, and other files. While R includes some basic functionality in it’s “base” packages, you often need to work with other packages when doing any data analysis.
In this tutorial you will use a group of packages collectively referred to as the **tidyverse**.
Before you can use a package, you need to download and install it. You can do this with code using the command `install.packages(“tidyverse”)`. It is usually not considered good practice to put this code in your script however. Instead, copy and paste it into the console, or use the
If you are using RStudio Desktop, you will need to install the package first. In which case, you Install button in the Packages tab and type the package name tidyverse.
2.7 Load the packages
Below you will see two boxes, one with comments and code, and the next with example console output. The console output is just there to show you what to expect to appear in the console when you run the given code. Console output in this tutorial always appears in a box below the code, and has two pound signs before each line like this: ##
Copy the code in the first box below (including the comments) and paste it into your script. You can copy the code by clicking the copy button in the upper right corner of the code box.
# load packages -----------------------------------------------------------
# run every time you restart RStudio
library(tidyverse) # load tidyverse, for working with datasets
library(palmerpenguins) # load palmerpenguins, our data source
Now run BOTH lines of code
Put your cursor on the first line and press Ctrl+Return to run it. The cursor automatically moves to the next line, so you can press Ctrl+Return again to run the next line.
You will see a warning and some messages in the Console tab, but those can be safely ignored.
You must run the library()
commands each time you start RStudio. That’s why it’s nice to keep that code at the very top of your script.
2.8 Assignment
- While the instructor helps everyone get caught up, familiarize yourself with the palmerpenguins package by reading the article Introduction to palmerpenguins
- In that article, find the code for “Scatterplot example 1”, copy it, and paste it into your R script
- Add code to add the following labels to the plot:
- title:
“Penguin flipper length versus body mass”
- x-axis:
“Body mass (g)”
- y-axis:
“Flipper length (mm)”
- caption:
“Created by YOUR NAME”
(replace YOUR NAME with your name)
- title:
- Export the figure (save it on your computer somewhere)
- Play around with different ways to export, like using the Export button in the Plots tab in RStudio, or just copying the image in that tab
- Then follow along as the instructor shows you a code based way of doing it.
- Create a new Microsoft Word document.
- Sign in to Microsoft 365 so you can save your document for later.
- Find the file on your computer, copy it, and paste it into the Word document. Alternatively, you could use the insert image button and dialog within Word to do the same thing.
- Save the document as a PDF
- Upload the document to the Lab 2 assignment on D2L.