Authoring with Code

GitHub Copilot

RStudio has a built-in connection to GitHub’s Copilot. Go to Tools > Global Options > Code > Copilot to enable it.

Data Frames

A great way to display data frames is by using df-print: paged in the YAML.

---
title: "Document"
format: 
   html:
     df-print: paged
---

For more details, see the Quarto documentation.

Here’s an example with the Palmer Penguins dataset:

library(palmerpenguins)
penguins

For more advanced tables, including search and filtering, see the DT (DataTables) package. Here’s an example using the datatable() function from the DT package to create an interactive table with filtering options:

library(DT)
datatable(penguins,
          filter = 'top')

Code output locations

A very useful feature of Quarto is the ability to control where code output appears. This can be used to create a variety of different layouts and formats. A particularly useful option is to place figures in the margin, which can be done using the column: margin option in the code chunk.

```{r}
#| label: fig-penguins
#| fig-cap: "Penguin bill length vs. body mass"
#| column: margin

library(ggplot2)
ggplot(penguins, aes(x = bill_length_mm, y = body_mass_g, color = species)) + 
  geom_point() +
  geom_smooth() +
  theme(legend.position = 'bottom')
```
Figure 1: Penguin bill length vs. body mass

For more details, see Margin Content in the Quarto documentation.

Code Annotation

library(tidyverse)
library(palmerpenguins)
1penguins |>
2  mutate(
    bill_ratio = bill_depth_mm / bill_length_mm,
    bill_area  = bill_depth_mm * bill_length_mm
  ) |>
3  select(species, island, bill_ratio, bill_area)
1
Take penguins, and then…
2
…add new columns for the bill ratio and bill area, and finally…
3
…select only the species, island, bill ratio, and bill area columns.

See Code Annotation for more details.

Code Listings

Code Listings

Conditional Content

Conditional Content