## This is an R comment. R knows this IS NOT code because it has a # in front!
## Code to load in packages we need
library(here)
Welcome to RStudio Cloud!
General information on Thursday labs
Each Thursday you will complete a lab in class with your group. Questions are selected from each lab to be turned in on Canvas (one submission per group). The questions to be submitted on Canvas are bolded in the lab. As you work through the lab with your group have the Canvas lab assignment open so that you can answer those questions as you go. Today’s activity is Lab 1 in Canvas for practice submitting as a group.
What is this document?
This is a Quarto document. Quarto is a simple formatting syntax for authoring HTML, PDF, and Microsoft Word documents.
We will always be using the “Visual” version of our Quarto document, which has options that should feel familiar.
R Code
You can differentiate the R code within a Quarto document from the body of the document, because it is contained in a grey box. Here is an example of an R code chunk:
The Quarto document knows this code is R code because the first thing inside the curly braces ({}
) is an r
. The first line of code below the {r}
is a code chunk option. You can tell something is a code chunk option because it starts with a hash-pipe (#|
). This option gives the code chunk a label–setup
. I like to name every code chunk with one word that represents what the code is doing. This also makes it easier to navigate between the different parts of your document!
If you wanted to insert a new R code chunk, you click on the “Insert” button, then select “Code Chunk”, and finally select R.
Question 1: Insert a new R code chunk below this question. Inside the code chunk include the following code: library(tidyverse)
Question 2: What do you think the library()
function in the above code does?
Question 3: What do you think the tidyverse is?
Rendering
When you click the Render button a document will be generated that includes both content of your document and any output from the R code chunks you included within the document.
Including Code
You can include code output in your knitted document:
glimpse(mpg)
Question 4: What do you think the glimpse()
function does?
Hint: What type of output does it give you?
Including Plots
You can also embed plots in the knitted document.
Here is an example of a plot:
Question 5: What do you think the echo: false
option does in the above code chunk?
Loading Data into R
You can load data files into R using functions from different packages. You will learn that the function you use depends on what format the data are in. Today, we will work with data that are stored in a Comma Separated Values file (a CSV). For this format of data, we will use the read_csv()
function to read in our data.
The data are located in the data folder of your project. To load in the data, we need to specify the “path” to the data, or where R needs to look for the data. I like to use the here()
function from the here package to read in data, because I find the format intuitive. I’ll walk you through what the code below is doing.
<- read_csv(
broadway here("resources",
"data",
"broadway.csv")
)
broadway
is the name of the object we are storing the dataset into- The
<-
“assigns” the output of theread_csv()
function into thebroadway
object read_csv()
is a function that reads in our csv data filehere()
is a function that specifies where the data live"data"
is the name of the folder the data live in"broadway.csv"
is the name of the dataset
Click on the broadway
dataset in the upper righthand corner to open up a data viewer for you to look over the dataset.
Question 6: Based on your inspection of the data, what are the observations / cases in these data?
Question 7: What are the variables in these data?
Lab Submission
Once you are done answering every question in the lab, hit the “Render” button to create an HTML document with your answers. To submit your HTML on Canvas you need to do the following:
- Check the box next to lab1.html
- Click on the “More” icon (with the gear by it)
- Select “Export…”
- You will be given an option to change the name of your document. I don’t. So, bypass this and click “Download”.
- Upload your downloaded Lab 1 file to the Lab 1 Canvas assignment