# Load a dataset into the R Environment.
data(ToothGrowth)
# Look at the summary of the variables in the dataset.
summary(ToothGrowth)
Lab 1: Introduction to Quarto
1 Part One: Workflow for GitHub
1.1 Step 1: Making a Copy from GitHub Classroom
For the first few weeks of the quarter, we will use GitHub Classroom to create our lab repositories. GitHub Classroom is a wonderful tool, as it makes it incredibly simple to copy the contents of a repository.
Use these steps to make a copy of the Lab 1 repository: List of Steps to Copy the Lab Assignment from GitHub Classroom
1.2 Step 2: Inspecting the Lab
Once you complete these steps, a dialogue box will open with messages about cloning (making a copy) of your Lab 1 repository. When the messages are finished, you should have a new RStudio session open. You should notice a few things:
- In the upper right corner you should see a blue R cube with
introdcution-to-quarto
written next to it , this tells you that you are working in an R Project (we’ll learn more about these next week). - In the “Files” pane, you should see a list of all the files included in the
lab-1
repository you copied. - In the upper right pane, you should see a tab labeled “Git”.
- Nothing should be displayed in this pane, since you haven’t made any changes to the files that were copied!
1.3 Step 3: Making a Small Change
Now, find the lab-1-student.qmd
file in the “Files” tab in the lower right hand corner. Click on this file to open it.
At the top of the document (in the YAML) there is an author
line that says "Your name here!"
. Change this to be your name and save your file either by clicking on the blue floppy disk or with a shortcut (command / control + s).
1.4 Step 4: Pushing Your Lab to GitHub
Now for our last step, we need to commit the files to our repo.
- Click the “Git” tab in upper right pane
- Check the “Staged” box for the
lab-1-student.qmd
file - Click “Commit”
- In the box that opens, type a message in “Commit message”, such as “Added my name”.
- Click “Commit”.
- Click the green “Push” button to send your local changes to GitHub.
RStudio will display something like:
>>> /usr/bin/git push origin HEAD:refs/heads/main
To https://github.com/atheobold/introduction-to-quarto-allison-theobold.git
3a2171f..6d58539 HEAD -> main
1.5 Step 5: Verifying Your Changes
Go back to your browser. I assume you’re still viewing the GitHub repo you just cloned. Refresh the page. You should see all the project files you committed there. If you click on “commits”, you should see one with the message you used, e.g. “Added my name”.
1.6 Step 6: Let’s get started working with Quarto!
Now that you’ve added your name, go ahead and get started with the rest of the lab!
2 Part Two: Exploring Quarto Documents
2.1 Specifying Global Execute Options
Execution options specify how the R code in your Quarto document should be displayed. This guide provides descriptions on the options you can specify in a document’s execution.
To start, your YAML should look something like this:
---
title: "Lab 1: Introduction to Quarto"
author: "Your name goes here!"
date: "Insert the date here!"
format: html
self-contained: true
editor: source
---
Question 1 – Add your name and the date to the YAML.
Question 2 – Add an execute
line to your YAML that includes the following options, (1) your source code is always output on the page, and (2) your document will render even if there are errors.
2.2 Running the Provided Code
Next, click on the “Play” button on the right of the first auto-populated code chunk. Alternatively, you can highlight (or simply put your cursor on the line of) the code you want to run and hit ctrl + Enter (for PCs) or ⌘ + Enter (for Macs).
You should see the code appear in the console, as well as the result of the code (2
). Keep in mind the [1]
before the 2
is vector notation. This means the result is a vector of length 1, whose first element is 2
.
Question 3 – Let’s spice this code up a bit. Delete 1 + 1
from the code chunk and paste in the following code:
Now run this code. You should see a six-number summary of the variables len
and dose
included in the ToothGrowth
dataset, as well as the frequency of the levels contained in the supp
variable. Further, if you inspect the Environment tab, the ToothGrowth
dataset should appear. You can click on the dataset name (not the blue play button!) to look at the data.
Check the Data Documentation
Question 4 – In your console (not in the Quarto document), type ?ToothGrowth
(or alternatively help(ToothGrowth)
). Use the information that pops up in the Help pane in RStudio to fill in the blanks below.
This dataset investigates the effect of __________ on tooth growth in _________________.
The two supplement delivery methods include OJ (______________) and VC (_______________).
ToothGrowth
is a data frame with ____ observations and ____ variables.
Question 5 – Before the code chunk, change the “Running the Provided Code” section header to a header that better describes the contents of this section (e.g., Tooth Growth Dataset).
2.3 Creating a Plot
Your second code chunk is just as boring as your first, so let’s spice it up!
Question 6 – Replace the plot(pressure)
code with the following (we will talk about graphics next week!):
library(tidyverse)
ggplot(data = ToothGrowth,
mapping = aes(x = supp, y = len)) +
geom_boxplot() +
labs(x = "Supplement", y = "Length of Teeth (mm)")
%>%
ToothGrowth mutate(dose = as.factor(dose)) %>%
ggplot(mapping = aes(x = dose, y = len)) +
geom_boxplot() +
labs(x = "Dose of Supplement (mg/day)", y = "Length of Teeth (mm)")
Now, run this code chunk! You should see two side-by-side boxplots, one plot comparing tooth length between the two supplement delivery methods and one plot comparing tooth length between the three different dosages uses.
Question 7 – Read over the Quarto documentation on figure options and add an option (#|
) to the above code chunk to make the figures print side-by-side (i.e., in one row) in the rendered HTML file.
Question 8 – Specify in the code chunk options that these boxplots should be center aligned.
Question 9 – Specify a figure caption for the boxplots (using a code-chunk option).
2.4 Inserting a New Code Chunk
Navigate to the last sentence of your the Quarto document. We’re now going to insert a new R
code chunk at the bottom of the document.
There are four different ways to do this:
Type ctrl + alt + i (on a PC) or ⌘ + ⌥ + i (on a Mac).
Click on the symbol. This should automatically default to R code, but if you have a Python compiler on your computer, you might need to select “R” from the options.
If you are using the Visual editor, click on the “Insert” button, then select “Code Chunk”, and finally select “R”.
Manually add the code chunk by typing
```{r}
. Make sure to close your code chunk with```
.
2.5 Conducting a t-test for Two Independent Samples
In this section, we are going to conduct a two-sample independent t-test to compare tooth length between the two supplement methods in the ToothGrowth
dataset. I have outlined the null and alternative hypotheses we will be testing:
\(H_0\): The treatment mean tooth length for the OJ supplement delivery method is the same as the treatment mean tooth length for the VC supplement delivery method.
\(H_A\): The treatment mean tooth length for the OJ supplement delivery method is different from the treatment mean tooth length for the VC supplement delivery method.
While a second course in statistics is a pre-requisite for this class, you may want to go here for a refresher on conducting two-sample independent t-tests.
Carry out the following steps:
Question 10 – Using the t.test()
function, write code to carry out the analysis. You can assume unequal variances and a two-sided alternative.
Look up the help documentation for t.test()
for directions on how your inputs should look. Hint: specifically look at the examples at the bottom for comparing extra
across groups in the sleep
dataset.
Question 11 – Run your code chunk to obtain the output for your statistical test.
Question 12 – Create a numbered list containing:
- Your conclusion (in the context of these data) based on the p-value.
- An interpretation of the confidence interval (make sure to read what confidence level is used by default).
Question 13 – Create another section header, describing the contents of this section.
2.6 Render Your Document
Render your document as an html file. Use the “Render” button (the blue arrow!) at the top of your screen.
If you run into trouble rendering your document, try restarting R and running your code chunks in order, and see if you can find the problem.
Another common issue is deleting the tick marks (```
) that surround your code chunks. If you notice that the code chunks are not showing a “Play” button (), or that they are not highlighted in gray, double check your tick marks!
Recall we included error: true
in our YAML execution options. This means that your document will still render even if there are errors. Make sure you are double checking your work!
You will notice that there is auto-generated text that is unrelated to the work that you completed. It is always a good idea to delete this extra text!
3 Challenge 1: Styling your Quarto Document
You can find a list of every option you can use to format an HTML document here and here. Further, here are lists of different themes you can specify in your YAML to produce differently styled outputs.
Make the following changes to your document:
Specify “code-folding” in your YAML document options.
Add a table of contents to your document.
Add a color theme to your document.
3.1 Render again!
Notice that when you render the document, all of the code reruns again, producing the same output as before, but with your changes—this is called reproducibility!
You should render often while completing your practice activities and lab assignments. Make small changes, then make sure the file still renders rather than making a bunch of big changes and then realizing something is wrong.
3.2 Turn it in!
Open the .html file on your computer to make sure it looks as you expected. Then upload the rendered (.html extension) document to Canvas!
You’ll be doing this same process for all your future Lab Assignments. Each of these will involve a Quarto file. Some weeks, I may have a template for you to copy like you had here, while other weeks you will generate your own Quarto file from scratch.