Markdown

Air has native support for formatting R code cells within Quarto, R Markdown, and Markdown files.

Quarto and R Markdown

By default, Air will automatically format both Quarto and R Markdown documents due to Air’s default-includes.

At the command line, the following will format R code cells within dplyr’s README.Rmd and all *.Rmd or *.qmd vignettes:

# Assume a working directory of ~/packages/dplyr
air format .

Or you can format a single document:

air format ~/packages/dplyr/vignettes/dplyr.Rmd

In VS Code and Positron, as long as you have configured your editor, then any Quarto or R Markdown documents will use Air to format R code cells on save.

To opt-out of Quarto and R Markdown formatting, exclude them in air.toml with:

[format]
exclude = [
  "*.[rR]md",
  "*.[qQ]md"
]

Markdown

Air does not format R code cells within Markdown documents by default. However, you can opt-in to Markdown support via the include option in air.toml:

[format]
include = ["*.md"]

Note that this will cause air format . to format every Markdown document within your project, including a README.md generated from README.Rmd, so you may need to add additional excludes or use a finer grained include, such as include = ["docs/*.md"] to avoid over formatting.

The testthat folder testthat/_snaps/, which holds generated snapshots as Markdown files, is automatically excluded by default-exclude.

Quarto options

Quarto has support for code cell options contained within a #| block at the start of the code cell, such as:

```r
#| label: fig-airquality
#| fig-cap: "Temperature and ozone level."
#| warning: false
library(ggplot2)

ggplot(airquality, aes(Temp, Ozone)) +
  geom_point() +
  geom_smooth(method = "loess")
```

Air’s R formatter does not see these code cell options and will not format them. However, Air will enforce a single blank line between the code cell options and the code, rewriting the above to:

```r
#| label: fig-airquality
#| fig-cap: "Temperature and ozone level."
#| warning: false

library(ggplot2)

ggplot(airquality, aes(Temp, Ozone)) +
  geom_point() +
  geom_smooth(method = "loess")
```

Skip comments

Within R code cells in Quarto and R Markdown files, you can use the standard # fmt: skip skip comments to skip the next syntax node. But sometimes (especially when creating presentations) you’d like to opt out of formatting and avoid showing the # fmt: skip comment. For that, you can use an HTML skip comment instead:

<!-- fmt: skip -->
```r
library(tidyverse)
library(palmerpenguins)
penguins |>                                      # <1>
  mutate(                                        # <2>
    bill_ratio = bill_depth_mm / bill_length_mm, # <2>
    bill_area  = bill_depth_mm * bill_length_mm  # <2>
  )
```

The <!-- fmt: skip --> comment tells Air to skip formatting of the next code block, which avoids corrupting the Quarto code annotations.

Any content placed between the skip comment and the code block will disable the comment. For example:

<!-- fmt: skip -->

This prose in between means the code block will be formatted.

```r
library(tidyverse)
library(palmerpenguins)
penguins |>                                      # <1>
  mutate(                                        # <2>
    bill_ratio = bill_depth_mm / bill_length_mm, # <2>
    bill_area  = bill_depth_mm * bill_length_mm  # <2>
  )
```

Note that in VS Code and Positron, HTML skip comments are only respected if you set the Air extension as your Quarto formatter instead of the Quarto extension. See the VS Code and Positron editor guide for more details.

Errors

Format on save

When using format on save in VS Code or Positron, Air will silently skip any R code cells with syntax errors, formatting all other valid R code cells.

CLI

At the command line, air format test.qmd will format all valid R code cells even if some other R code cells have syntax errors. If there are any R code cells with syntax errors, then these will be loudly reported as errors and will cause Air to exit with a non-zero exit code.

Additionally, air format test.qmd --check or air format . --check will fail with a non-zero exit code if there are any R code cells with syntax errors.

If you purposefully have code cells with syntax errors (such as ones marked with eval: false), then you’ll also need to add a <!-- fmt: skip --> comment above them to force Air to skip them.

Full Markdown formatter

Air is tightly scoped to only formatting the R code cells within your Quarto, R Markdown, and Markdown documents. If you’re looking for a Quarto or Markdown formatter that also formats prose, consider using panache or rumdl, both of which can be configured to call Air on embedded R code cells.

If you use one of these, you’ll want to turn off Air’s native formatting with:

[format]
exclude = [
  "*.[rR]md",
  "*.[qQ]md"
]