<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/4-ways-to-be-more-efficient-using-rstudios-code-snippets-with-11-ready-to-use-examples/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://jozefhajnala.gitlab.io/r/r906-rstudio-snippets/"> Jozef's Rblog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h1>Introduction</h1>
<p>In this post we will look at yet another productivity increasing feature of the RStudio IDE – Code Snippets. Code Snippets let us easily insert and potentially execute predefined pieces of code and work not just for R code, but many other languages as well.</p>
<blockquote>
<p>In this post we will cover 4 different ways to increase productivity using Code Snippets and provide 11 real-life examples of their use that you can take advantage of instantly.</p>
</blockquote>
</div>
<div>
<h1>How do Code Snippets work</h1>
<div>
<h2>Using, Viewing and editing snippets</h2>
<ol>
<li>In RStudio, we can browse and define snippets under <code>Tools -> Global Options... -> Code -> Edit Snippets</code> window</li>
<li>When typing code, the snippet will appear as an auto-complete option (similar to function names) if we type the first few letters of its name</li>
<li>Use <code>Shift+Tab</code> to insert the snippet immediately or pick the snippet from the auto-complete list (by clicking or scrolling on it and pressing <code>Tab</code>)</li>
</ol>
<blockquote>
<p>Note that as there is no auto-completion when editing R Markdown documents, we need to use the <code>Shift+Tab</code> method exclusively in that case.</p>
</blockquote>
</div>
<div>
<h2>Sharing and exporting Snippets</h2>
<p>Once we customize some snippets they are automatically saved to our <code>~/.R/snippets</code> directory, one file per language. We can use these files to share our snippets with others, but also to edit them directly in the respecive file, without the need to click through the RStudio menus. RStudio will automatically load the Snippets from the <code>.snippets</code> files first, for languages that have the file present.</p>
</div>
</div>
<div>
<h1>Four common use-case scenarios</h1>
<div>
<h2>1. Automatically insert boilerplate or template-style code</h2>
<p>The first and probably most frequent use of the Code Snippets feature is to quickly insert predefined pieces of code that require a lot of typing with little alternation, a.k.a. boilerplate code. A good illustration is a snippet covering a <code>tryCatch</code> block:</p>
<pre><code>snippet tryc
    ${1:variable} &lt;- tryCatch({
        ${2}
    }, warning = function(w) {
        message(sprintf(&quot;Warning in %s: %s&quot;, deparse(w[[&quot;call&quot;]]), w[[&quot;message&quot;]]))
        ${3}
    }, error = function(e) {
        message(sprintf(&quot;Error in %s: %s&quot;, deparse(e[[&quot;call&quot;]]), e[[&quot;message&quot;]]))
        ${4}
    }, finally = {
        ${5}
    })</code></pre>
<blockquote>
<p>Note that the snippet definition is intended using <code></code></blockquote></div></div>