Snippets: the RStudio feature that saves time and reduces errors

TL;DR — RStudio has a built-in snippet editor (Tools → Global Options → Code → Edit Snippets) that most people never open. You can save your go-to plot templates there like volcano plots, PCAs, heatmap themes… but also your named color palettes and the file paths you type out from memory every week (reference genome, annotation files, whatever). Give them all the same prefix and you get a searchable personal cheat sheet you can expand from any R session.

The thing we kept doing

Every project, same routine: need a volcano plot, go find the last project where you made one, copy the block, paste it, rename the columns, fix the colors you forgot you’d changed. Repeat for the PCA plot. Repeat for the heatmap theme you like.

None of that is hard. It’s just tedious, and it means your “default” plot style quietly drifts between projects because you’re copying from whatever script happens to be open.

The feature that’s already there

RStudio has had a snippet system for years, tucked under Tools → Global Options → Code → Edit Snippets. It opens a plain text file where you define a trigger word and a block of code. Type the trigger, hit Tab, and RStudio expands it inline, with placeholders you tab through to fill in.

A volcano plot snippet looks like this:

snippet volcano
	ggplot(${1:df}, aes(x = ${2:log2FC}, y = -log10(${3:padj}))) +
		geom_point(aes(color = sig), size = 1.5, alpha = 0.7) +
		scale_color_manual(values = c("grey70", "firebrick")) +
		theme_minimal(base_size = 13) +
		labs(x = "log2 fold change", y = "-log10(adj. p-value)")

Type volcano, press Tab, and you get the whole block with df, log2FC, and padj as tab stops you jump through and fill in — no copy-pasting, no hunting through old scripts.

It’s not just plots

The same mechanism works for anything you type from memory more than twice. Two we use constantly:

Named color palettes, so you stop redefining the same cell-type colors in every script:

snippet cheat_palette_single_cell
	my_pal <- c(Tcell = "#4E79A7", Bcell = "#F28E2B", Myeloid = "#E15759",
		        NK = "#76B7B2", Other = "#BAB0AC")

Paths you always need and never remember exactly, like a reference genome:

snippet cheat_ref_genome_GRCh38_fa
	"/data/references/GRCh38/GRCh38.primary_assembly.genome.fa"

No more digging through shell history or an old script to find where that file actually lives.

The prefix trick

Once you’ve got more than a handful of these, give them all the same prefix, or use two or three categories, for example we use cheat_, so it’s cheat_palette_single_cell, cheat_heatmap, cheat_ref_genome, and so on. Type cheat and RStudio’s autocomplete shows you every custom snippet you’ve saved, regardless of what it actually does. It turns the whole thing into one searchable list instead of something you have to remember the exact trigger word for.

Two things that trip people up the first time

  • The indentation has to be a literal tab character, not spaces. If you paste a snippet from somewhere and it doesn’t expand, that’s almost always why — RStudio’s snippet parser is picky about this.
  • ${1:...} are tab stops, not just placeholder text. The number controls the order you tab through them, and $0 (if you add it) marks where the cursor lands after the last one. Worth using for anything with more than one thing you’d normally have to edit by hand.

Worth doing if you catch yourself retyping the same thing twice

It’s a small thing, but once you’ve got your volcano plot, your palettes, and your most-used paths as cheat_ snippets, a lot of the “which project did I copy this from again” friction just disappears. And because it’s a plain text file, you can drop it in your dotfiles and it follows you to the next machine — which is more than you can say for “the script where I think I had the good version.” Basically a personal cheat sheet, in a format every R session already knows how to read.