R tip: Save time with RStudio code snippets

In this fifth episode of Do More with R, Sharon Machlis, director of Editorial Data & Analytics at IDG Communications, shows how code snippets work in RStudio—and how you can make your own

Hi, this is Sharon Machlis, Director of Editorial Data & Analytics at IDG Communications. I’m here with episode 5 of Do More With R: Save Time With RStudio Code Snippets.
Code snippets is another phrase for macros or templates. They’re a way to store, well, snippets of code that you’re likely to want to re-use. Most software for writing code, or IDEs, have a version of these. RStudio has them, too. Let me show you how they work – and how you can make your own.
I’ll start off by importing data I used in the last episode, IT Manager salaries by year. Here’s what a default ggplot bar graph of that data looks like.
And, what I want my final graph to look like.
I spent a fair amount of time tweaking that graph code. I changed bar colors, added labels, included commas on the y axis, centered the title and subtitle …. I. Will. Never. Remember. How. To. Do. This. Again.
I could save the graph in a file, and hope I remember where the file is. Or, I guess I could make a function with all these defaults, but that gets tricky if I want to do a bit more tweaking next time.
Instead, I made a code snippet.
I’ll open a new file so you can see how this works. RStudio also has built-in snippets, so I name all of mine starting with my_underscore. If I start typing that
You can see I’ve got a lot of them in the dropdown list. I called my bar graph snippet my_custom_barchart. I’ll type that, select it, and you see what pops up. All my code for the graph.
But not only the code. There are variables in this snippet. My cursor automatically jumps to the first variable, which I called mydataframe. I’ll change that to the name of my data frame, salaries. If I hit the tab key, my cursor jumps to the next variable, which I called myxcol. I’ll change that to Year. I’ll hit tab again, and notice what happened this time. There are two places where myycol variable are used. When I change that to Salary on the first one, the second one changes too.
And, I can hit tab again two more times to get to my title and subtitle variables.
Now if I run this code, I get a graph in my customized format.
Code snippets live in a special RStudio text file that you can get to with the menu commands
But you don’t have to go through 4 menu layers to get to the snippet file. The usethis package has a function edit_rstudio_snippets(). With autocompletion, I find that easier.
I put my snippet at the top of the file (the ones below it are included with RStudio). This is the format. Each snippet starts with the word snippet at the beginning of a line, followed by a space, and then the name of the snippet. All the snippet code below has to be indented with a tab. If you use spaces to indent the code, the snippet won’t work.
Otherwise, you mostly write the code you want, as usual. Once each code line starts with a tab, it can have additional spaces.
To add a variable, you start with a dollar sign and open curly brace, then the number of your variable, a colon, the name of the variable, and the closing brace. You can see that in the first code line of my snippet, where I defined variables mydataframe, myxcol, and myycol. Your cursor will jump from variable to variable based on the numbers of the variables: Here 1, 2, and 3. If you’re using a variable more than once, you give it the same number and name in multiple places in your code.
Save that r snippet file, and you’re done.
And, I’m done for this episode. Thanks for watching! For more R tips, head to the More With R video page at That’s https B I T period L Y slash more with R, all lowercase except for the R. So long, and hope to see you next episode!