do_something_very_complicated(
that = "requires",
many = arguments,
some = "of which may be very long"
)Spring 2026 | Data 2 (399)
Jeffrey M. Girard | Lecture 06a
Style your code to improve organization and clarity
Manage the behavior of individual code chunks in Quarto
Style describes optional changes
e.g., how to name files and objects
e.g., where to add spaces and line breaks
But using a consistent code style has benefits
Code becomes more readable and predictable
Collaboration becomes easier and smoother
File names should be meaningful and clear
models.qmd ➔ dissertation_study2_models.qmdAvoid special characters in names (arguably this includes spaces)
M@$teR$ Th3s1s.r ➔ masters_thesis.RIf your files need to be run in order, prefix with numbers
1_import.qmd, 2_model.qmd, 3_visualize.qmdUse zero-padding as necessary (to sort them properly)
01_download.qmd, 02_tidy.qmd, …, 10_visualize.qmdUse sections and subsections to give the file internal structure
Load all packages together at the top of the document
Use comments to explain the “why” (not the “what” or “how”)
countydata and plot_countycounty_data and county_plotcountyData and countyPlotx ➔ heart_ratedf ➔ aim1_dataF, T, c, mean, sum, pi, data1/2+3*4-5 ➔ 1 / 2 + 3 * 4 - 5a>3&b<=0 ➔ a > 3 & b <= 05 ^ 2 ➔ 5^2( 1 + 2 ) * 3 ➔ (1 + 2) * 3sum ( sales ) ➔ sum(sales)df $ variable ➔ df$variablec(1 ,2,3 ,4, 5) ➔ c(1, 2, 3, 4, 5)round() function has two arguments:x contains the number(s) to be rounded (a data argument)digits contains the number of digits to round to (a details argument)x =details =round(x = 2 / 3, digits = 1) ➔ round(2 / 3, digits = 1)Line break and indent after assignment
Follow each pipe with a line break
Separate long lines of arguments with line breaks
Indent further for arguments
author: or format: fields#| field: value```{r}
#| label: summarize-data
#| echo: false
# Regular R code safely goes below the options!
summary(mtcars)
```
Danger
Do not include empty lines or R comments above the hash pipes.
Quarto will stop reading options as soon as it hits normal code or empty space.
Sometimes a chunk will issue messages, which we may want to hide in our rendered Quarto output file.
Loading required package: nlme
Attaching package: 'nlme'
The following object is masked from 'package:dplyr':
collapse
This is mgcv 1.9-4. For overview type '?mgcv'.
This is often a side-effect of loading packages. Some packages like tidyverse give easy ways to suppress this, but others don’t.
We can add a hashpipe and set message to false.
```{r}
#| message: false
library(mgcv)
```
Produces:
Warnings are somewhere between messages and errors.
Errors will usually prevent Quarto from rendering at all. But sometimes (e.g., for teaching) we do want to render them.
```{r}
#| error: true
x <- 1 2 3 4 5
```
Produces:
Sometimes we want to show code without running it. This is helpful for showing examples that take too long to run or for demonstrating syntax.
```{r}
#| eval: false
remove.packages("tidyverse")
```
Produces:
Sometimes we want to run the code without showing it (i.e., without echoing it). This is very common in reports where the audience only cares about the final results, plot, or table.
```{r}
#| echo: false
summary(mtcars$mpg)
```
Produces:
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.40 15.43 19.20 20.09 22.80 33.90
An alternative to echo is to let the user fold/unfold the code.
```{r}
#| code-fold: true
summary(mtcars$mpg)
```
Produces:
By default, Quarto puts your code and its output into separate blocks.
We can use the collapse option to merge them into a single, compact block.
```{r}
#| collapse: true
summary(mtcars$mpg)
```
Produces:
We can give each code chunk a unique name using the label option.
```{r}
#| label: import-data
aim1_data <- read_csv("aim1_data.csv")
```
The Setup Label
The “setup” label is special! RStudio will automatically run the chunk with this label before running any other chunk in interactive mode. Load your packages and data in this chunk.
We can use hash pipes to control how generated plots are displayed in our rendered document. Helpful options include fig-width, fig-height, fig-align, and fig-cap.
```{r}
#| label: mpg-plot
#| echo: false
#| fig-width: 6
#| fig-height: 4
#| fig-align: left
#| fig-cap: "Miles per gallon by weight."
mtcars |> ggplot(aes(x = wt, y = mpg)) + geom_point()
```
Miles per gallon by weight and horsepower.