<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://damouzo.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://damouzo.github.io/" rel="alternate" type="text/html" /><updated>2026-09-11T13:13:54+00:00</updated><id>https://damouzo.github.io/feed.xml</id><title type="html">Daniel Mouzo</title><subtitle>Portfolio and blog of Daniel Mouzo, a senior bioinformatician working across cancer genomics, gene therapy, single-cell multiomics, and workflow engineering.</subtitle><author><name>Daniel Mouzo</name></author><entry><title type="html">Snippets: the RStudio feature that saves time and reduces errors</title><link href="https://damouzo.github.io/blog/r-snippets/" rel="alternate" type="text/html" title="Snippets: the RStudio feature that saves time and reduces errors" /><published>2026-09-10T00:00:00+00:00</published><updated>2026-09-10T00:00:00+00:00</updated><id>https://damouzo.github.io/blog/r-snippets</id><content type="html" xml:base="https://damouzo.github.io/blog/r-snippets/"><![CDATA[<p><strong>TL;DR</strong> — 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.</p>

<h2 id="the-thing-we-kept-doing">The thing we kept doing</h2>

<p>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.</p>

<p>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.</p>

<h2 id="the-feature-thats-already-there">The feature that’s already there</h2>

<p>RStudio has had a snippet system for years, tucked under <strong>Tools → Global Options → Code → Edit Snippets</strong>. 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.</p>

<p>A volcano plot snippet looks like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>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)")
</code></pre></div></div>

<p>Type <code class="language-plaintext highlighter-rouge">volcano</code>, press Tab, and you get the whole block with <code class="language-plaintext highlighter-rouge">df</code>, <code class="language-plaintext highlighter-rouge">log2FC</code>, and <code class="language-plaintext highlighter-rouge">padj</code> as tab stops you jump through and fill in — no copy-pasting, no hunting through old scripts.</p>

<h2 id="its-not-just-plots">It’s not just plots</h2>

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

<p><strong>Named color palettes</strong>, so you stop redefining the same cell-type colors in every script:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>snippet cheat_palette_single_cell
	my_pal &lt;- c(Tcell = "#4E79A7", Bcell = "#F28E2B", Myeloid = "#E15759",
		        NK = "#76B7B2", Other = "#BAB0AC")
</code></pre></div></div>

<p><strong>Paths you always need and never remember exactly</strong>, like a reference genome:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>snippet cheat_ref_genome_GRCh38_fa
	"/data/references/GRCh38/GRCh38.primary_assembly.genome.fa"
</code></pre></div></div>

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

<h2 id="the-prefix-trick">The prefix trick</h2>

<p>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 <code class="language-plaintext highlighter-rouge">cheat_</code>, so it’s <code class="language-plaintext highlighter-rouge">cheat_palette_single_cell</code>, <code class="language-plaintext highlighter-rouge">cheat_heatmap</code>, <code class="language-plaintext highlighter-rouge">cheat_ref_genome</code>, and so on. Type <code class="language-plaintext highlighter-rouge">cheat</code> 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.</p>

<h2 id="two-things-that-trip-people-up-the-first-time">Two things that trip people up the first time</h2>

<ul>
  <li><strong>The indentation has to be a literal tab character</strong>, 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.</li>
  <li><strong><code class="language-plaintext highlighter-rouge">${1:...}</code> are tab stops, not just placeholder text.</strong> The number controls the order you tab through them, and <code class="language-plaintext highlighter-rouge">$0</code> (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.</li>
</ul>

<h2 id="worth-doing-if-you-catch-yourself-retyping-the-same-thing-twice">Worth doing if you catch yourself retyping the same thing twice</h2>

<p>It’s a small thing, but once you’ve got your volcano plot, your palettes, and your most-used paths as <code class="language-plaintext highlighter-rouge">cheat_</code> 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.</p>]]></content><author><name>Daniel Mouzo</name></author><category term="tips-and-tricks" /><category term="r" /><summary type="html"><![CDATA[RStudio's built-in snippet editor lets you save your most-used ggplot templates, color palettes, and reference paths, and expand them with a few keystrokes. A quick look at a feature that's been there the whole time.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://damouzo.github.io/images/socialCard.png" /><media:content medium="image" url="https://damouzo.github.io/images/socialCard.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>