Using RStudio Code Snippets Including Quarto
Sep 11, 2022
R
Snippets for R
The RStudio (integrated development environment, IDE) has built-in code snippets that can save you from having to remember syntax details and reduce unnecessary typing. For example, to create a new function in an R script, you can type fun
and then push the tab (or shift tab) key and fun
will be replaced with:
name <- function(variables) {
}
You can see the built-in snippets by doing these steps:
- From the Tools Menu choose Global Options…
- Click on the Code windowpane
- Click the Edit Snippets button
The snippets window initially shows you the R snippets. Scroll through them and you will see small time savers like lib
which is converted to library()
and for fans of base R, there are shortcuts that add up for real savings, like the apply
family of functions and the if
, el
and ie
functions. You will notice that you need to use shift tab to trigger the two-letter shortcuts.
Adding Snippets
You can add your own snippets or grab mine from the GitHub gists linked at the bottom of this page. I normally use dplyr
to clean/process my data, so I prefer to use a case_when()
function instead of if()
, else()
, else if()
functions for “trapping” bad values and writing instructions to clean up the problems. I added a snippet called mcw (which stands for mutate case when):
snippet mcw
mutate(${1:variable} =
case_when(
${2:condition} ~ ${3:value},
${4:condition} ~ ${5:value},
TRUE ~ ${6:otherwise}
)
)
It autocompletes to:
mutate(variable =
case_when(
condition ~ value,
condition ~ value,
TRUE ~ otherwise
)
)
You will notice that the new variable name is selected automatically. So you will be able to type the new variable name immediately, then push tab and the focus will shift to the first logic check. Type it then push tab again to shift focus to the new value for your variable. Use tab a couple more times to enter options for the second logic check and finally, you can tab to the “otherwise” option which will be used for all records that were not handled by the conditions you wrote above. Yes, you can add as many conditions as you like. You just need to remember to type a ~
and a ,
for every additional rule.
R Snippets for R Markdown and Quarto
All of the R snippets work inside of code chunks in both R Markdown and Quarto documents.
Working with Quarto
Setting Options
Quarto (which, for R users, is basically R Markdown 2.0) uses a #|
“hash pipe” to set each option. These belong at the top of R code blocks. I have added snippets for common code blocks. hp
expands to #| option
with the focus set on option so you can just type the option which you want to set.
Other shortcuts include:
hpl
which creates a code block label/name#| label:
hpf
which adds options for a code block for a figure that has: a label for cross references, a caption to appear below the figure, an alternative text description for people using screen readers, and an option to turn off the rendering of the code in the output (which can be turned back on with the wordtrue
)#| label: fig- #| fig-cap: #| fig-alt: #| echo: false
hpt
which adds options for a code block for a table that has: a label for cross references, a caption to appear below the table, and an option to turn off the display of the code (which can be turned back on with the wordtrue
)#| label: #| tbl-cap: #| echo: false
Quarto Syntax
RStudio also lets you set options for Markdown, R Markdown and Quarto documents (outside of the code blocks). To view the ones that are built-in, you need to do one extra step:
- From the Tools Menu choose Global Options…
- Click on the Code windowpane
- Click the Edit Snippets button
- Click on the Markdown windowpane
I have a shortcut for the letter r
(followed by shift tab) which turns into an R code block with a label:
```{r}
#| label: label
```
Other useful snippets include shortcuts to call-out blocks. These are the colored notes with special symbols that are common in O’Reilly and “For Dummies” books. I use these as I am taking notes. You can set the appearance option to be default
, simple
, or minimal
and you make the icons disapear with callout-icon=false
.
The con
snippet makes a callout “note”:
:::{.callout-note appearance=minimal}
:::
The cot
snippet makes a callout “tip”:
:::{.callout-tip appearance=simple}
:::
The cow
snippet makes a callout “warning”:
:::{.callout-warning appearance=simple}
:::
There are two other snippets that are particularly useful for making slides. The two
snippet adds the syntax for a slide with two columns:
:::: {.columns}
::: {.column width="50%"}
:::
::: {.column width="50%"}
:::
::::
Do notice that the outer “fence” has four colons ::::
. That divider could be done with three colons but I find it easier to read like this.
The note
snippet adds the syntax to add a section of speaker notes for a presentation slide.
::: {.notes}
:::
My GitHub Gists
I put my code snippets on GitHub.
The gist for R can be found here and the gist for R Markdown/Quarto is here. You can use the three- or four-step instructions above to augment/replace the built in snippets.