Git, Quarto, and R
January 12, 2025
These slides address the following items.
{tidyverse}
.{tidyverse}
?New
File -> New Project
to begin creating a new R projectFile -> New File -> Quarto Document
to create a new Quarto notebookFile -> Save As...
to save the file – give it a name like CrashCourse.qmd
and leave it in the project directoryHead back to your web browser and refresh your repo – your files have arrived!
Quarto documents consist of three main components
We’ll generally copy/paste the same YAML header (I’ll send you a slightly more complex template in Slack now)
Code chunks must have executable R code in them
Text/markdown cells can have anything you like
Here’s a Quarto cheatsheet, but feel free to work with your favorite LLM to get Quarto to do exactly what you want
For the rest of the slide deck, we’ll transition to working in R but then come back to Git/GitHub at the end of the class meeting
Students coming from MAT300: Feel free to grab the data and start investigating
Question for New Students: Do you want to partner up with an existing student to work and learn from them, or do you want to continue with the slides?
There are three main dialects in R
Base-R
data.table
(speed)
Tidy-R / tidyverse
(readability and consistency)
Note: R dialects just refers to how we choose to write R code and which functions we prioritize – dialects can be (and often are) mixed.
install.packages("PACKAGE_NAME")
to install a package
library(PACKAGE_NAME)
to load the package
\(\bigstar\) Install the {tidyverse}
and load it in your Quarto Notebook
\(\bigstar\) While you are at it, install the {skimr}
, {tidymodels}
, {patchwork}
, and {kableExtra}
packages as well – {skimr}
and {kableExtra}
are the only ones you might want to load for now
ASIDE: We store objects in R with the arrow operator (<-
)
Reading Data: To read data, we use a function of the form read_*()
.
{tidyverse}
(or at least {readr}
) to be loaded{readxl}
or {haven}
are common)\(\bigstar\) Read the MLB batted balls data and the park dimensions data into your Quarto Notebook
I’ll post the links in Slack
head()
to view first six rowsglimpse()
to view dimensions and data typesskim()
from {skimr}
for much more detail\(\bigstar\) Try these functions on your battedballs
data
02:00
%>%
or |>
) make code more readable and allow chaining of functions together\(\bigstar\) Rewrite the functions you used to explore your data with pipes
filter()
to return only desired records (by a conditional statement)select()
to return only desired columns (by name, separated by commas)summarize()
to compute summaries on a tablegroup_by()
to create groups in a tablemutate()
to create new columns or change existing ones\(\bigstar\) How might we use these functions? Write down some questions that could be answered using the functions described above. Start with a couple very simple questions and then work up to questions whose answers might be more complex to find.
filter()
to return only desired records (by a conditional statement)select()
to return only desired columns (by name, separated by commas)summarize()
to compute summaries on a tablegroup_by()
to create groups in a tablemutate()
to create new columns or change existing ones\(\bigstar\) How might we use these functions? Write down some questions that could be answered using the functions described above. Start with a couple very simple questions and then work up to questions whose answers might be more complex to find.
\(\bigstar\) We’ll try answering some of those questions now!
penguins
data frame, and thenNote: penguins
data frame is not permanently altered here
Now the change is permanent because we’ve stored the result
Notice the use of the arrow operator (<-
)
Be careful overwriting existing objects – think about whether you:
Use this time to continue playing with the MLB data sets
Use the blue render button to convert your markdown document into a beautiful HTML document and enjoy the fruits of your labor!
Write down and answer additional interesting questions that might use functionality discussed in this slide deck – start simple and then build up to questions that might be more complex
Document your work by including text descriptions alongside the code chunks
Don’t worry if your document looks quite plain for now, our next class meeting is devoted to using markdown syntax in Quarto effectively
Quarto, Inline R, and semi-automated reporting