<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/customizing-styler-the-quick-way/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://lorenzwalthert.netlify.com/posts/customizing-styler-the-quick-way/"> Lorenz Walthert</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<blockquote>
<p><em>I am currently experiencing problems with getting my posts in full length on<br>
r-bloggers. You can continue <a rel="nofollow" href="https://lorenzwalthert.netlify.com/" target="_blank">here</a> with<br>
reading in case only the first paragraph is rendered.</em></p>
</blockquote>
<p>One cool thing that happens if you work resonates in the community is that you<br>
see other people using it. In this blog post I am going to address a typical<br>
question people have when they want to use a source code formatter – in<br>
particular styler:</p>
<blockquote>
<p><em>I don’t like rule xyz of the tidyverse style guide, which is the default<br>
style guide implemented in styler. How can I tell styler not to apply it?</em></p>
</blockquote>
<h1>Theory</h1>
<p>First, I think reading the docs would be a good approach. There are two<br>
resources:</p>
<ul>
<li>The <a rel="nofollow" href="http://styler.r-lib.org/reference/tidyverse_style.html" target="_blank">help file</a><br>
for the function <code>tidyverse_style()</code>, which returns the transformer functions that prettify your code. It has a few interesting arguments, some of which are rather complex.<sup><a rel="nofollow" href="https://lorenzwalthert.netlify.com/posts/customizing-styler-the-quick-way/#fn:One-example-is-m" target="_blank">1</a></sup></li>
<li>If you can’t get styler behaving the way you want using the arguments of<br>
<code>tidyverse_style()</code>, you have another option, which is described in a <a rel="nofollow" href="http://styler.r-lib.org/articles/customizing_styler.html" target="_blank">vignette</a>: Creating your own style guide. Yes, I admit, it’s pretty long and if you don’t want to become a <em>styler expert</em>, it may be a little bit overwhelming.</li>
</ul>
<p>If you don’t care about how to create new rules but you simply want to <em>remove</em><br>
a rule, I have good news for you: There is a quick way to do it. These are the<br>
steps you need to complete in order to do it:</p>
<ul>
<li>Figure out which transformer function in the transformers returned<br>
by <code>tidyerse_style()</code> corresponds to the rule you want to remove.</li>
<li>Set that element in the list to <code>NULL</code>, which is equivalent to removing it.</li>
<li>Pass the list to <code>style_text</code> as a transformer.</li>
</ul>
<h1>Practice</h1>
<p>Lets assume you want to remove the rule that turns <code>=</code> into <code>&lt;-</code> for assignment.<br>
That means you want <code>string = &quot;hi there&quot;</code></p>
<p>to remain unchanged after applying styler. This is not the case if you use the<br>
default style guide of styler:</p>
<pre><code>library(styler)
style_text(&quot;string = 'hi there'&quot;)
</code></pre>
<pre><code>string &lt;- &quot;hi there&quot;
</code></pre>
<p>So you need to figure out which rule is responsible for this. Let’s check the<br>
transformer categories used with the tidyverse style guide.</p>
<pre><code>transformers &lt;- tidyverse_style()
names(transformers)
</code></pre>
<pre><code>## [1] &quot;initialize&quot;        &quot;line_break&quot;        &quot;space&quot;            
## [4] &quot;token&quot;             &quot;indention&quot;         &quot;use_raw_indention&quot;
## [7] &quot;reindention&quot;
</code></pre>
<p>From the aforementioned<br>
<a rel="nofollow" href="http://styler.r-lib.org/articles/customizing_styler.html" target="_blank">vignette</a>:</p>
<blockquote>
<p>We note that there are different types of transformer functions. initialize<br>
initializes some variables in the nested parse table (so it is not actually a<br>
transformer), and the other elements modify either spacing, line breaks or<br>
tokens. use_raw_indention is not a function, it is just an option.</p>
</blockquote>
<p>Now, we can look at the names of the rules that are sub-elements of the<br>
transformer categories.</p>
<pre><code>library(styler)
purrr::modify_depth(transformers, 1, names)
</code></pre>
<pre><code>## $initialize
## [1] &quot;initialize&quot;
## 
## $line_break
## [1] &quot;remove_line_break_before_curly_opening&quot;            
## [2] &quot;remove_line_break_before_round_closing_after_curly&quot;
## [3] &quot;remove_line_break_before_round_closing_fun_dec&quot;    
## [4] &quot;style_line_break_around_curly&quot;                     
## [5] &quot;set_line_break_after_opening_if_call_is_multi_line&quot;
## [6] &quot;set_line_break_before_closing_call&quot;                
## [7] &quot;remove_line_break_in_empty_fun_call&quot;               
## [8] &quot;add_line_break_after_pipe&quot;                         
## 
## $space
##  [1] &quot;indent_braces&quot;                     
##  [2] &quot;unindent_fun_dec&quot;                  
##  [3] &quot;indent_op&quot;                         
##  [4] &quot;indent_eq_sub&quot;                     
##  [5] &quot;indent_without_paren&quot;              
##  [6] &quot;fix_quotes&quot;                        
##  [7] &quot;remove_space_before_closing_paren&quot; 
##  [8] &quot;remove_space_before_opening_paren&quot; 
##  [9] &quot;add_space_after_for_if_while&quot;      
## [10] &quot;add_space_before_brace&quot;            
## [11] &quot;remove_space_before_comma&quot;         
## [12] &quot;style_space_around_math_token&quot;     
## [13] &quot;style_space_around_tilde&quot;          
## [14] &quot;spacing_around_op&quot;                 
## [15] &quot;spacing_around_comma&quot;              
## [16] &quot;remove_space_after_opening_paren&quot;  
## [17] &quot;remove_space_after_excl&quot;           
## [18] &quot;set_space_after_bang_bang&quot;         
## [19] &quot;remove_space_before_dollar&quot;        
## [20] &quot;remove_space_after_fun_dec&quot;        
## [21] &quot;remove_space_around_colons&quot;        
## [22] &quot;start_comments_with_space&quot;         
## [23] &quot;remove_space_after_unary_pm_nested&quot;
## [24] &quot;spacing_before_comments&quot;           
## [25] &quot;set_space_between_levels&quot;          
## [26] &quot;set_space_between_eq_sub_and_comma&quot;
## 
## $token
## [1] &quot;force_assignment_op&quot;                   
## [2] &quot;resolve_semicolon&quot;                     
## [3] &quot;add_brackets_in_pipe&quot;                  
## [4] &quot;remove_terminal_token_before_and_after&quot;
## [5] &quot;wrap_if_else_multi_line_in_curly&quot;      
## 
## $indention
## [1] &quot;update_indention_ref_fun_dec&quot;
## 
## $use_raw_indention
## NULL
## 
## $reindention
## [1] &quot;indention&quot;     &quot;comments_only&quot;
</code></pre>
<p>Spotted the rule we want to get rid of? It’s under <code>token</code> and it’s called<br>
<code>force_assignment_op</code>. I agree, we could have chosen a better name. If you are<br>
not sure if you can guess from the name of the rule what it does you can also<br>
have a look at the function declaration of this (unexported) function.</p>
<pre><code>styler:::force_assignment_op
</code></pre>
<pre><code>## function (pd) 
## {
##     to_replace &lt;- pd$token == &quot;EQ_ASSIGN&quot;
##     pd$token[to_replace] &lt;- &quot;LEFT_ASSIGN&quot;
##     pd$text[to_replace] &lt;- &quot;&lt;-&quot;
##     pd
## }
## </code></pre>