QuartoBasics

What You See

Render

Click on Render to run

Render Options

Change to “Preview in Viewer Pane”

Render Outputs

In the same folder of the .qmd file with .html extension:

YAML Block

  • format options, such as html, pdf, docx, beamer (slides)

  • toc: true (create a table of content)

  • author, such as author: "Amir Karami"

Quarto

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

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code.

You can embed code like this:

1 + 1
[1] 2

You can add options (click on the setting icon) to executable code (not showing the code) by adding {r echo=FALSE}.

like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.1     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.2
✔ purrr     1.2.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
smaller <- diamonds |> 
  filter(carat <= 2.5)

smaller |> 
  ggplot(aes(x = carat)) + 
  geom_bar(binwidth = 0.01)
Warning in geom_bar(binwidth = 0.01): Ignoring unknown parameters: `binwidth`

If we don’t want to not show the figure and show just code eval=FALSE

#echo=FALSE means d
library(tidyverse)
smaller <- diamonds |> 
  filter(carat <= 2.5)

smaller |> 
  ggplot(aes(x = carat)) + 
  geom_bar(binwidth = 0.01)

Change figure sizes

library(tidyverse)
smaller <- diamonds |> 
  filter(carat <= 2.5)

smaller |> 
  ggplot(aes(x = carat)) + 
  geom_bar(binwidth = 0.01)
Warning in geom_bar(binwidth = 0.01): Ignoring unknown parameters: `binwidth`

{#fig-height: 2, fig-width: 5 width=672}

Go to the folder of .qmd file. There is a folder based on the NameOfFile_files, like QuartoBasics_files, which includes all created files, like figure-html.

Figure

Method 1

Copy and paste an image. For example, search in Google for RStudio logo, click on Images, right click on one of the images, and paste it here. Like,

This images will be saved in a images folder.

Method 2

Insert > Figure/Image > find the file or add URL > OK

Interactive Images

More available at https://rstudio.github.io/dygraphs/index.html

data(economics)
#install.packages("dygraphs")
#install.packages("xts")

library(dygraphs)
library(xts)

colnames(economics)
[1] "date"     "pce"      "pop"      "psavert"  "uempmed"  "unemploy"
# Convert to xts or time series format
ts_data <- xts(economics[, c("unemploy", "pop")], order.by = economics$date)

dygraph(ts_data)

Table

Create this table:

Project Phase Deadline
Team Creation Mar 31
Data Apr 6
Goal Apr 13

By default, Quarto prints data frames and matrices as you’d see them in the console:

mtcars[1:5, ]
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

If you prefer that data be displayed with additional formatting you can use the knitr::kable() function

knitr::kable(mtcars[1:5, ], )
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2

This looks better! But the column names aren’t super clear! Let’s look at just the first four columns and see how we can change their names:

subcars <- mtcars[1:5,1:4]
knitr::kable(
  subcars,
  col.names = c("Miles per Gallon","Number of Cylinders",
                "Engine Displacement","Horsepower"),
  row.names = F,
  caption = "More Beautiful Car Data"
)
More Beautiful Car Data
Miles per Gallon Number of Cylinders Engine Displacement Horsepower
21.0 6 160 110
21.0 6 160 110
22.8 4 108 93
21.4 6 258 110
18.7 8 360 175

Now, besides just showing tables of raw data, let’s say we wanted to present the mean, median, and standard deviations of penguin bill length across the species of penguins in a nice table.

library(tidyverse)
summary_statistics <- palmerpenguins::penguins |>
  dplyr::group_by(species) |>
  dplyr::summarize(Mean = mean(bill_length_mm,na.rm=T),
                   Median = median(bill_length_mm,na.rm=T),
                   `Standard Deviation` = sd(bill_length_mm,na.rm=T))

knitr::kable(summary_statistics,
             caption="Summary Statistics for Penguin Bill Length by Species",
             digits = 4)
Summary Statistics for Penguin Bill Length by Species
species Mean Median Standard Deviation
Adelie 38.7914 38.80 2.6634
Chinstrap 48.8338 49.55 3.3393
Gentoo 47.5049 47.30 3.0819

Now suppose we wanted to run a simple linear regression to assess the relationship between penguin bill length and bill depth. Typically, the results of regression are presented in an ANOVA type of table:

slr <- summary(lm(bill_length_mm ~ bill_depth_mm, data=palmerpenguins::penguins))
knitr::kable(slr$coefficients)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 55.0673698 2.5159514 21.887295 0.00e+00
bill_depth_mm -0.6498356 0.1457327 -4.459093 1.12e-05

Let’s clean this up a bit and round the numbers:

slr <- summary(lm(bill_length_mm~bill_depth_mm,data=palmerpenguins::penguins))
knitr::kable(round(slr$coefficients, digits = 2))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 55.07 2.52 21.89 0
bill_depth_mm -0.65 0.15 -4.46 0

Interactive Tables

There are lots of other packages which can help control aesthetic features of a table as well such as DT and xtable among many others. For example, with DT, we can create interactive tables when we render to HTML. More available at https://rstudio.github.io/DT/.

#install.packages('DT')
library(DT)
datatable(mpg)

Footnote

Click on here to go to KSU1 website.

Footnotes

  1. KSU is a university in GA.↩︎