Quarto & R Demo

Suggested Answers

Quarto is great

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Render

When you click the Render button a document will be generated that includes both content and the output of embedded code. Note: if something is wrong with your code, your document will not render.

Showing the Margin

It is not professional to have your code run off the page. We are going to have R display the margin so we can avoid this in our rendered documents!

  1. Go to Tools
  2. Global Options
  3. Click Code
  4. Click Display
  5. Click Show Margin (Should have 80 margin column)

Notice the line appear? It may be faint, but it’s important. If your code (not writing) goes beyond this, it will appear off the page causing you to lose points on assignments (and your document will look sloppy).

Packages for class

── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0     ✔ purrr   0.3.5
✔ tibble  3.1.8     ✔ dplyr   1.0.9
✔ tidyr   1.2.1     ✔ stringr 1.4.1
✔ readr   2.1.3     ✔ forcats 0.5.2
Warning: package 'ggplot2' was built under R version 4.2.2
Warning: package 'tidyr' was built under R version 4.2.2
Warning: package 'readr' was built under R version 4.2.2
Warning: package 'purrr' was built under R version 4.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()

What is a package? Why is it important we library packages?

Packages in R have a lot of different functions that we want to use!

The term library = unpack! This tells R we want to use the functions inside the packages

How to take notes for class

– These AEs are for YOU

– Use # within a code chunk to comment codes

– Scratch paper

mtcars

For the remainder of the class, we will use the mtcars data set. This is a prebuilt data set in R. The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).

To see more about these data, please visit here: https://www.rdocumentation.org/packages/datasets/versions/3.6.2/topics/mtcars

  • Take a glimpse of the data set using the glimpse function in R

Before we use this function, run ?glimpse in the console below. What happens?

glimpse(mtcars)
Rows: 32
Columns: 11
$ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,…
$ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,…
$ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16…
$ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180…
$ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,…
$ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.…
$ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18…
$ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,…
$ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,…
$ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,…
$ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,…

You can run code in a few different ways. You can click the green arrow in the code chunk; use a keyboard short cut (Ctrl+Enter for PC; Cmd+Return for Mac). For a list of other keyboard shortcuts, please visit the following: https://support.posit.co/hc/en-us/articles/200711853-Keyboard-Shortcuts-in-the-RStudio-IDE

I HIGHLY suggest using the posted cheat sheets throughout the session linked here. Before answering the next question, please follow the link and download the dplyr cheat sheet (top right).

  • Now, filter out the any cars who weigh more than 4000 lbs in a single pipeline. Note, wt is input in 1000s of lbs in the data set.

Things to note:

  • What is our data set name?

mtcars

  • What is our variable?

wt

  • What is our objective?

*Remove cars that are more than 4000 lbs**

mtcars |> 
  filter(wt > 4)
                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4

What does “in a single pipeline” mean?

No “breaks” in our code

  • Notice how the data were not overwritten by running mtcars in the console.

Now, filter out the any cars who weigh more than 4000 lbs and save the new data set named large_cars.

large_cars <- mtcars |>
  filter(wt > 4)
  • Using your new data set, take the mean weight of cars using the summarise function. Report the mean below. Hint, look up the help file to the function, and scroll down to the examples. You are also encouraged to use the cheat sheet.
large_cars |> 
  summarise(avg = mean(wt))
      avg
1 5.02225

Let’s make a plot!

A large part of this course is going to focus on data visualization. Today we are going to make our first plot in R. You are encouraged to download the ggplot cheat sheet (top left). Additionally, we will be using this resource: https://ggplot2.tidyverse.org/reference/

Let’s make a plot of mpg. What are some plots that would be appropriate to create?

We could make a boxplot! Or a histogram.

First Plot

Let’s walk through the steps to create a boxplot. For this exercise, we will use the mtcars data set.

mtcars |>
  ggplot(
    aes(x = mpg)
  ) + 
  geom_boxplot()