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:

  1. From the Tools Menu choose Global Options…
  2. Click on the Code windowpane
  3. Click the Edit Snippets button

Screenshot showing the Code windowpane from Global Options window. Arrows point to the Code windowpane in the upper left of the picture and the Edit Snippets button near the bottom.

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:

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:

  1. From the Tools Menu choose Global Options…
  2. Click on the Code windowpane
  3. Click the Edit Snippets button
  4. Click on the Markdown windowpane

Screenshot showing the Edit Snippet Window with an arrow pointing to 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.