Introduction to Quarto

RStudio

Warning

If you’re new to R, follow the online ModernDrive chapter https://moderndive.netlify.app/1-getting-started.html

Using Project

GIF for creating an RStudio Project

RStudio Layout

Locate the following panes:

  • Console
  • Source
  • History
  • Help

R Basics

Install R Packages

See https://twitter.com/visnut/status/1248087845589274624

# Install the tidyverse "meta" package
# install.packages("tidyverse")
# Install the here package
# install.packages("psych")

!!! Don’t you dare include any install.packages() statements in your homework !!!

Load a Package

# Uncomment the code below to load the tidyverse package
# library(tidyverse)

Built-In Data

data(bfi, package = "psych")  # make the "bfi" data set available for use
head(bfi, 10)  # show the first 10 rows of data

Quarto

https://quarto.org/

  • Open-source scientific and technical publishing system
  • Next-generation version of R Markdown

Elements of Quarto

See https://quarto.org/docs/get-started/hello/rstudio.html

  • YAML header
  • Code chunks
  • Markdown text

YAML

Exercise 1
  • Update your name in the author field
  • Change the option from toc: false to toc: true
  • Insert today’s date using the date field

Markdown Text

See https://quarto.org/docs/authoring/markdown-basics.html

  • Bold **Bold**

  • italic *italic*

  • code `code`

  • Link to USC [Link to USC](https://www.usc.edu)

  • Header

# Level 1

## Level 2

### Level 3

Unordered list

  • item 1
  • item 2
    • item 2a

Ordered list

  1. item 1
  2. item 2
    1. item 2a

Equations (LaTeX)

Inline: \(Y_i = \beta_0 + \beta_1 X_i + e_i\)

Display:

\[\rho = \frac{\tau^2}{\tau^2 + \sigma^2}\]

Inline Code

The value of $\pi$ is `r pi`

The value of \(\pi\) is 3.1415927

Code Chunks

Content to be interpreted by R engine

1 + 1
[1] 2
v1 <- c(1, 2, 6, 8)  # create a vector `v1`
v1[3]  # extract 3rd element of v1
[1] 6
# extract the `A2` column, and compute the median
median(bfi$A2)
[1] NA

Chunk Options

  • echo: false: Do not show the input command
  • fig-width, fig-height: Size of figure
library(ggplot2)
ggplot(bfi, aes(x = A2)) +
    geom_bar()
Warning: Removed 27 rows containing non-finite values (`stat_count()`).

Exercise 2

Change the aspect ratio of the graph above to 1:1

Rendering

  • format: html, pdf, docx, etc
    • If running into problems for PDF, enter the following to the Terminal:
    Terminal
    quarto install tool tinytex
  • Try also revealjs!

Note: Different R sessions are used for the console and for knitting

Exercise

Download the Rmd file, then do the following

  1. Complete Ex1 above.
  2. Complete Ex2 above.
  3. Type this equation in LaTeX: https://www.gstatic.com/education/formulas2/472522532/en/cronbach_s_alpha.svg. Make sure you get all the subscripts right.
  4. Install and load the modelsummary package, run the following, and write down what the chunk does. You’ll need to set eval: true (or remove the line) so the chunk runs.
# Install and load the modelsummary package first; otherwise, it won't run
library(dplyr)
library(modelsummary)
datasummary(O1 + O2 + O3 + O4 + O5 ~ 
                Factor(gender, levelnames = c("Males", "Females")) * 
                (Mean + SD),
            data = bfi)

  1. Run the following and find out what the code chunk does.
bfi %>%
    select(A1:C5) %>%
    datasummary_correlation()
  1. Run the following and find out what the code chunk does.
bfi %>%
    select(E1:E5) %>%
    psych::alpha()

  1. Run the following and find out what this code chunk does.
library(tidyr)
bfi %>%
    select(N1:N5) %>%
    pivot_longer(cols = N1:N5, names_to = "item", values_to = "score") %>%
    ggplot(aes(x = score)) +
    geom_bar() +
    facet_wrap(~ item)

  1. Render the document to a PDF output.

  2. Submit the knitted PDF to Blackboard.