“Code snippets” is just another phrase for macros or templates—and a great way to save time when writing scripts. Snippets not only cut down on keystrokes while you’re coding; they also save you search time trying to dig up that complex code you got just right six months ago but no longer remember.

Here’s how they work—and how you can make your own—in RStudio.

Sample graph

Since I get a little tired of the ubiquitous mtcars and iris data sets for examples, I’ll use some information about IT Manager salaries (from a Computerworld salary survey) for this demo. Here’s my data frame if you’d like to follow along.


salaries <- data.frame(
Year = c("2017", "2016", "2015", "2014", "2013", "2012") ,
Salary = c(99053L, 96413L, 95619L, 92724L, 91686L, 90165L)
)