<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/february-17-tips-and-tricks/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.rstudio.com/rviews/2017/03/03/february-17-tips-and-tricks/"> RStudio</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>

<p>by Sean Lopp</p>
<p>If you spend time with an excellent programmer, one thing that immediately jumps out is how quickly she can write code. It often appears to be magic, the number of keystrokes simply can’t equal the number of characters on the screen. The secret: it doesn’t! Most programmers use a series of tricks to save the hassle of writing boilerplate code. This month, my personal goal was to spend less time typing, even if it required slowing down to master these tricks. In this post, I’ll focus on one of the easiest and most flexible tools at your disposal: <a target="_blank" rel="nofollow" href="https://support.rstudio.com/hc/en-us/articles/204463668-Code-Snippets">code snippets</a>. (Astute readers will note that I’ve mentioned code snippets <a target="_blank" rel="nofollow" href="https://www.rstudio.com/rviews/2016/11/11/easy-tricks-you-mightve-missed/">before</a>. They deserve the extra attention!)</p>
<p>Code snippets are a common programming tool similar to keyboard shortcuts. A snippet of text, usually a few characters long, is used as a shortcut to create a larger piece of boilerplate code. <code>Tab</code> navigation enables jumping through the boilerplate code to locations where customization is required. </p>
<p>For example, one of my favorite code snippets is for creating a function. The template of an R function looks like:</p>
<pre><code>functionName &lt;- function(arguments) {
  code 
}
</code></pre>
<p>The boilerplate – not worth typing – is:</p>
<pre><code> &lt;- function( ) {

}
</code></pre>
<p>In RStudio, the code snippet for functions is <code>fun</code>. Typing <code>fun</code> and pressing <code>Tab</code> will provide an auto-complete suggestion for the snippet. Typing <code>fun</code> and pressing <code>Shift + Tab</code> inserts the snippet directly. As a general typing rule, keeping your hands on the middle row of the keyboard will speed things up. I hit <code>Shift</code> with my right pinky and <code>Tab</code> with my left. </p>
<p>After inserting the boilerplate code, the cursor is placed to left of the <code>&lt;-</code>. You can then quickly type the name of the function. Pressing <code>Tab</code> places the cursor in between the parenthesis. Pressing <code>Tab</code> again places the cursor in between the brackets. This allows you to fill in the remaining portions of the function template quickly.</p>
<p><strong>Type <code>fun</code> and <code>Shift + Tab</code>:</strong></p>
<pre><code>HERE &lt;- function() {

}
</code></pre>
<p><strong>Pressing <code>Tab</code> Once</strong></p>
<pre><code> &lt;- function(HERE) {

}
</code></pre>
<p><strong>Pressing <code>Tab</code> Twice</strong></p>
<pre><code> &lt;- function() {
 HERE
 }
</code></pre>
<h2>Accessing Snippets</h2>
<p>RStudio comes with many built-in snippets. To access these snippets, navigate to <code>Tools</code> -> <code>Global Options</code> -> <code>Code</code> and press <code>Edit Snippets</code>.</p>
<p><img alt="" src="https://i2.wp.com/www.rstudio.com/wp-content/uploads/2017/03/SL_code_snippets.png?w=456" data-recalc-dims="1"></p>
<p>The menu will show all of the defined snippets. Consider the definition for the function snippet:</p>
<pre><code>snippet fun
    ${1:name} &lt;- function(${2:variables}) {
        ${0}
    }
</code></pre>
<p>The snippet starts by defining the character shortcut <code>fun</code>. The definition includes the boilerplate code. The <code>Tab</code> behavior is defined using <code>${1}</code> notation. The <a target="_blank" rel="nofollow" href="https://support.rstudio.com/hc/en-us/articles/204463668-Code-Snippets">code snippet</a> help page describes the syntax in more detail.</p>
<h2>A Few Examples</h2>
<h3><code>ts</code></h3>
<p>Adds a time-stamped comment.</p>
<pre><code># Mon Feb 20 07:34:03 2017 ------------------------------
</code></pre>
<h3><code>if</code></h3>
<p>Adds the skeleton of an <code>if</code> statement</p>
<pre><code>if () {

}
</code></pre>
<h3><code>mat</code></h3>
<p>Adds the skeleton for defining a matrix.</p>
<pre><code>matrix( , nrow = , ncol = )
</code></pre>
<p>There are code snippets for other languages as well.</p>
<h2>Creating Snippets</h2>
<p>Code snippets are powerful because they can be customized and created. If you find yourself repeatedly typing the same code, create a snippet. The menu of snippets in RStudio is editable. To create or modify a snippet, add your changes to the menu and click <code>Save</code>.</p>
<h2>Sharing Snippets</h2>
<p>Once you’ve created a handful of useful code snippets, it would be generous to share. The <a target="_blank" rel="nofollow" href="https://github.com/dgrtwo/snippr"><code>snippr</code> package</a> makes this process easy.</p>
<p>This <a target="_blank" rel="nofollow" href="https://gist.github.com/slopp/27903daaf9fbd41afc73442b600a7618">gist</a> contains two of my favorites: a code snippet for creating a single file Shiny app, and a code snippet to start a ggplot.</p>
<p>Shiny:</p>
<pre><code>snippet shiny
    library(shiny)
    ui &lt;- fluidPage(
        ${1:ui}
    )

    server &lt;- function(input, output, session){
      ${2:server}
    }
    shinyApp(ui = ui, server = server)
</code></pre>
<p>ggplot2:</p>
<pre><code>snippet gg
    ggplot(${1:data}, aes(${2:aes})) + 
      geom_${3:geom}()
</code></pre>
<p>What are your favorite snippets?</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.rstudio.com/rviews/2017/03/03/february-17-tips-and-tricks/"> RStudio</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/WNus4b86NW0"></a></div>