<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/styler-a-non-invasive-source-code-formatter-for-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://lorenzwalthert.github.io/stylerpost/"> Rsome - A blog about some R stuff</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I am pleased to announce that the R package<br>
<a rel="nofollow" href="https://github.com/r-lib/styler" target="_blank">styler</a>, which I have worked on<br>
through <a rel="nofollow" href="https://lorenzwalthert.github.io/gsoc2017/" target="_blank">Google Summer of Code 2017</a><br>
with <a rel="nofollow" href="https://twitter.com/krlmlr" target="_blank">Kirill Müller</a> and <a rel="nofollow" href="https://twitter.com/xieyihui" target="_blank">Yihui Xie</a>,<br>
has reached a mature stage.</p>
<p>You can now install it from CRAN</p>
<figure>
<pre><code data-lang="r"><span>install.packages</span><span>(</span><span>&quot;styler&quot;</span><span>)</span></code></pre>
</figure>
<p>If your CRAN mirror does not yet have it, you can get it from GitHub with <code>remotes::install_github(&quot;r-lib/styler&quot;)</code>.</p>
<p>The package formats R code, by default according to the <a rel="nofollow" href="http://style.tidyverse.org/" target="_blank">tidyverse style guide</a>.<br>
The distinguishing feature of styler is its flexibility. We will introduce some<br>
of the options below. Before I continue, I want to thank my two mentors<br>
from Google Summer of Code, in particular<br>
<a rel="nofollow" href="https://twitter.com/krlmlr" target="_blank">Kirill Müller</a>, who was an amazing<br>
companion during the three months of coding – and beyond. I feel really blessed<br>
how everything came about. In addition, I would like to thank Google for<br>
organizing GSOC this year and facilitating the involvement of students in open<br>
source projects.</p>
<p>Back to the package: <code>styler</code> can style text, single files, packages and entire<br>
R source trees with the following functions:</p>
<ul>
<li><code>style_text()</code> styles a character vector.</li>
<li><code>style_file()</code> styles R and Rmd files.</li>
<li><code>style_dir()</code> styles all R and/or Rmd files in a directory.</li>
<li><code>style_pkg()</code> styles the source files of an R package.</li>
<li>An RStudio Addin that styles the active file R or Rmd file, the current<br>
package or the highlighted code.</li>
</ul>
<h1>Styling options</h1>
<p>We can limit ourselves to styling just spacing information by indicating this<br>
with the <code>scope</code> argument:</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>&quot;styler&quot;</span><span>)</span><span><br data-jekyll-commonmark-ghpages=""></span><span>library</span><span>(</span><span>&quot;magrittr&quot;</span><span>)</span><span><br data-jekyll-commonmark-ghpages=""></span><span>style_text</span><span>(</span><span>&quot;a=3; 2&quot;</span><span>,</span><span> </span><span>scope</span><span> </span><span>=</span><span> </span><span>&quot;spaces&quot;</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">a = 3; 2</code></pre>
</figure>
<p>If you are reading this post on r-bloggers, there might be issues with<br>
displaying code and rendered output correctly. You can continue reading on<br>
the page this post was published <a rel="nofollow" href="https://lorenzwalthert.github.io/stylerpost/" target="_blank">initially</a>.</p>
<p>Or, on the other extreme of the scale, styling spaces, indention, line breaks<br>
and tokens:</p>
<figure>
<pre><code data-lang="r"><span>style_text</span><span>(</span><span>&quot;a=3; 2&quot;</span><span>,</span><span> </span><span>scope</span><span> </span><span>=</span><span> </span><span>&quot;tokens&quot;</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">a &lt;- 3<br data-jekyll-commonmark-ghpages="">2</code></pre>
</figure>
<p>Another option that is helpful to determine the level of ‘invasiveness’ is<br>
<code>strict</code>. If set to <code>TRUE</code>, spaces and line breaks before or after tokens are<br>
set to either zero or one. However, in some situations this might be<br>
undesirable (so we set <code>strict = FALSE</code>), as the following example shows:</p>
<figure>
<pre><code data-lang="r"><span>style_text</span><span>(</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>&quot;data_frame(<br data-jekyll-commonmark-ghpages=""> small = 2 ,<br data-jekyll-commonmark-ghpages=""> medium = 4,#comment without space<br data-jekyll-commonmark-ghpages=""> large =6<br data-jekyll-commonmark-ghpages=""> )&quot;</span><span>,</span><span> </span><span>strict</span><span> </span><span>=</span><span> </span><span>FALSE</span><span><br data-jekyll-commonmark-ghpages=""></span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">data_frame(<br data-jekyll-commonmark-ghpages="">  small  = 2,<br data-jekyll-commonmark-ghpages="">  medium = 4, # comment without space<br data-jekyll-commonmark-ghpages="">  large  = 6<br data-jekyll-commonmark-ghpages="">)</code></pre>
</figure>
<p>We prefer to keep the equal sign after “small”, “medium” and large aligned,<br>
so we set <code>strict = FALSE</code> to set spacing to <em>at least</em> one around <code>=</code>.</p>
<p>Though simple, hopefully the above examples convey some of the flexibility of<br>
the configuration options available in <code>styler</code>. You can find out more about<br>
options available with the tidyverse style by checking out the help file for<br>
<code>style_tidyverse()</code>.</p>
<h1>Gallery</h1>
<p>In the sequel, let us focus on a configuration with<br>
<code>strict = TRUE</code> and <code>scope = &quot;tokens&quot;</code> and illustrate a few more examples of<br>
code before and after styling.</p>
<p><code>styler</code> can identify and handle unary operators and other math tokens:</p>
<figure>
<pre><code data-lang="styler"># Before<br data-jekyll-commonmark-ghpages="">1++1-1-1/2</code></pre>
</figure>
<figure>
<pre><code data-lang="text"># After<br data-jekyll-commonmark-ghpages="">1 + +1 - 1 - 1 / 2</code></pre>
</figure>
<p>This is tidyverse style. However, styler offers very granular control for<br>
math token spacing. Assuming you like spacing around <code>+</code> and <code>-</code>, but not<br>
around <code>/</code> and <code>*</code> and <code>^</code>. This can be achieved as follows:</p>
<figure>
<pre><code data-lang="r"><span>style_text</span><span>(</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>&quot;1++1/2*2^2&quot;</span><span>,</span><span> <br data-jekyll-commonmark-ghpages=""> </span><span>math_token_spacing</span><span> </span><span>=</span><span> </span><span>specify_math_token_spacing</span><span>(</span><span>zero</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;'/'&quot;</span><span>,</span><span> </span><span>&quot;'*'&quot;</span><span>,</span><span> </span><span>&quot;'^'&quot;</span><span>))</span><span><br data-jekyll-commonmark-ghpages=""></span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">1 + +1/2*2^2</code></pre>
</figure>
<p>It can also format complicated expressions that involve line breaking and<br>
indention based on both brace expressions and operators:</p>
<figure>
<pre><code data-lang="styler"># Before<br data-jekyll-commonmark-ghpages="">if (x >3) {stop(&quot;this is an error&quot;)} else {<br data-jekyll-commonmark-ghpages="">c(there_are_fairly_long,<br data-jekyll-commonmark-ghpages="">1 / 33 * <br data-jekyll-commonmark-ghpages="">2 * long_long_variable_names)%>% k(<br data-jekyll-commonmark-ghpages=""><br data-jekyll-commonmark-ghpages="">) }</code></pre>
</figure>
<figure>
<pre><code data-lang="text"># After<br data-jekyll-commonmark-ghpages="">if (x > 3) {<br data-jekyll-commonmark-ghpages="">  stop(&quot;this is an error&quot;)<br data-jekyll-commonmark-ghpages="">} else {<br data-jekyll-commonmark-ghpages="">  c(<br data-jekyll-commonmark-ghpages="">    there_are_fairly_long,<br data-jekyll-commonmark-ghpages="">    1 / 33 *<br data-jekyll-commonmark-ghpages="">      2 * long_long_variable_names<br data-jekyll-commonmark-ghpages="">  ) %>% k()<br data-jekyll-commonmark-ghpages="">}</code></pre>
</figure>
<p>Lines are broken after <code>(</code> if a function call spans multiple lines:</p>
<figure>
<pre><code data-lang="styler"># Before<br data-jekyll-commonmark-ghpages="">do_a_long_and_complicated_fun_cal(&quot;which&quot;, has, way, to, <br data-jekyll-commonmark-ghpages="">                              &quot;and longer then lorem ipsum in its full length&quot;<br data-jekyll-commonmark-ghpages="">                              )</code></pre>
</figure>
<figure>
<pre><code data-lang="text"># After<br data-jekyll-commonmark-ghpages="">do_a_long_and_complicated_fun_cal(<br data-jekyll-commonmark-ghpages="">  &quot;which&quot;, has, way, to,<br data-jekyll-commonmark-ghpages="">  &quot;and longer then lorem ipsum in its full length&quot;<br data-jekyll-commonmark-ghpages="">)</code></pre>
</figure>
<p><code>styler</code> replaces <code>=</code> with <code>&lt;-</code> for assignment, handles single quotes within<br>
strings if necessary, and adds braces to function calls in pipes:</p>
<figure>
<pre><code data-lang="styler"># Before<br data-jekyll-commonmark-ghpages="">one= 'one string'<br data-jekyll-commonmark-ghpages="">two= &quot;one string in a 'string'&quot;<br data-jekyll-commonmark-ghpages="">a %>%<br data-jekyll-commonmark-ghpages="">  b %>%<br data-jekyll-commonmark-ghpages="">  c</code></pre>
</figure>
<figure>
<pre><code data-lang="text"># After<br data-jekyll-commonmark-ghpages="">one &lt;- &quot;one string&quot;<br data-jekyll-commonmark-ghpages="">two &lt;- &quot;one string in a 'string'&quot;<br data-jekyll-commonmark-ghpages="">a %>%<br data-jekyll-commonmark-ghpages="">  b() %>%<br data-jekyll-commonmark-ghpages="">  c()</code></pre>
</figure>
<p>Function declarations are indented if multi-line:</p>
<figure>
<pre><code data-lang="styler"># Before<br data-jekyll-commonmark-ghpages="">my_fun &lt;- function(x, <br data-jekyll-commonmark-ghpages="">y, <br data-jekyll-commonmark-ghpages="">z) {<br data-jekyll-commonmark-ghpages="">  just(z)<br data-jekyll-commonmark-ghpages="">}</code></pre>
</figure>
<figure>
<pre><code data-lang="text"># After<br data-jekyll-commonmark-ghpages="">my_fun &lt;- function(x,<br data-jekyll-commonmark-ghpages="">                   y,<br data-jekyll-commonmark-ghpages="">                   z) {<br data-jekyll-commonmark-ghpages="">  just(z)<br data-jekyll-commonmark-ghpages="">}</code></pre>
</figure>
<p><code>styler</code> can also deal with tidyeval syntax:</p>
<figure>
<pre><code data-lang="styler"># Before<br data-jekyll-commonmark-ghpages="">mtcars %>%<br data-jekyll-commonmark-ghpages="">  group_by(!!!my_vars)</code></pre>
</figure>
<figure>
<pre><code data-lang="text"># After<br data-jekyll-commonmark-ghpages="">mtcars %>%<br data-jekyll-commonmark-ghpages="">  group_by(!!! my_vars)</code></pre>
</figure>
<p>If you, say, don’t want comments starting with <code>###</code> to be indented, you can<br>
formulate an unindention rule:</p>
<figure>
<pre><code data-lang="r"><span>style_text</span><span>(</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>c</span><span>(</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>&quot;a &lt;-function() {&quot;</span><span>,</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>&quot;### not to be indented&quot;</span><span>,</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>&quot;# indent normally&quot;</span><span>,</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>&quot;33&quot;</span><span>,</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>&quot;}&quot;</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>),</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>reindention</span><span> </span><span>=</span><span> </span><span>specify_reindention</span><span>(</span><span>regex_pattern</span><span> </span><span>=</span><span> </span><span>&quot;###&quot;</span><span>,</span><span> </span><span>indention</span><span> </span><span>=</span><span> </span><span>0</span><span>)</span><span><br data-jekyll-commonmark-ghpages=""> <br data-jekyll-commonmark-ghpages=""></span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">a &lt;- function() {<br data-jekyll-commonmark-ghpages="">### not to be indented<br data-jekyll-commonmark-ghpages="">  # indent normally<br data-jekyll-commonmark-ghpages="">  33<br data-jekyll-commonmark-ghpages="">}</code></pre>
</figure>
<h1>Customizing styler – implementing your own style guide</h1>
<p>Not only can you customize styler with the options of <code>tidyverse_style()</code>. The<br>
real flexibility of <code>styler</code> is supporting third-party style<br>
guides. Technically speaking, a style guide such as <code>tidyverse_style()</code> is<br>
nothing but a set of transformer functions and options. How you can create<br>
your own style guide is explained in <a rel="nofollow" href="http://styler.r-lib.org/articles/customizing_styler.html" target="_blank">this</a><br>
vignette.</p>
<h1>Wrap-up</h1>
<p>I hope I have convinced you that you should give <code>styler</code> a try. If you find<br>
unexpected behavior, you are welcome to file an issue on<br>
<a rel="nofollow" href="https://github.com/r-lib/styler/issues" target="_blank">GitHub</a>.</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://lorenzwalthert.github.io/stylerpost/"> Rsome - A blog about some R stuff</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/l0hqtRkL-xo" height="1"></a></div>