Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I am currently experiencing problems with getting my posts in full length on r-bloggers. You can continue here with reading in case only the first paragraph is rendered.
One cool thing that happens if you work resonates in the community is that you see other people using it. In this blog post I am going to address a typical question people have when they want to use a source code formatter – in particular styler:
I don’t like rule xyz of the tidyverse style guide, which is the default style guide implemented in styler. How can I tell styler not to apply it?
Theory
First, I think reading the docs would be a good approach. There are two resources:
- The help file
for the function
tidyverse_style()
, which returns the transformer functions that prettify your code. It has a few interesting arguments, some of which are rather complex.1 - If you can’t get styler behaving the way you want using the arguments of
tidyverse_style()
, you have another option, which is described in a vignette: Creating your own style guide. Yes, I admit, it’s pretty long and if you don’t want to become a styler expert, it may be a little bit overwhelming.
If you don’t care about how to create new rules but you simply want to remove a rule, I have good news for you: There is a quick way to do it. These are the steps you need to complete in order to do it:
- Figure out which transformer function in the transformers returned
by
tidyerse_style()
corresponds to the rule you want to remove. - Set that element in the list to
NULL
, which is equivalent to removing it. - Pass the list to
style_text
as a transformer.
Practice
Lets assume you want to remove the rule that turns =
into <-
for assignment.
That means you want string = "hi there"
to remain unchanged after applying styler. This is not the case if you use the default style guide of styler:
library(styler) style_text("string = 'hi there'") string <- "hi there"
So you need to figure out which rule is responsible for this. Let’s check the transformer categories used with the tidyverse style guide.
transformers <- tidyverse_style() names(transformers) ## [1] "initialize" "line_break" "space" ## [4] "token" "indention" "use_raw_indention" ## [7] "reindention"
From the aforementioned vignette:
We note that there are different types of transformer functions. initialize initializes some variables in the nested parse table (so it is not actually a transformer), and the other elements modify either spacing, line breaks or tokens. use_raw_indention is not a function, it is just an option.
Now, we can look at the names of the rules that are sub-elements of the transformer categories.
library(styler) purrr::modify_depth(transformers, 1, names) ## $initialize ## [1] "initialize" ## ## $line_break ## [1] "remove_line_break_before_curly_opening" ## [2] "remove_line_break_before_round_closing_after_curly" ## [3] "remove_line_break_before_round_closing_fun_dec" ## [4] "style_line_break_around_curly" ## [5] "set_line_break_after_opening_if_call_is_multi_line" ## [6] "set_line_break_before_closing_call" ## [7] "remove_line_break_in_empty_fun_call" ## [8] "add_line_break_after_pipe" ## ## $space ## [1] "indent_braces" ## [2] "unindent_fun_dec" ## [3] "indent_op" ## [4] "indent_eq_sub" ## [5] "indent_without_paren" ## [6] "fix_quotes" ## [7] "remove_space_before_closing_paren" ## [8] "remove_space_before_opening_paren" ## [9] "add_space_after_for_if_while" ## [10] "add_space_before_brace" ## [11] "remove_space_before_comma" ## [12] "style_space_around_math_token" ## [13] "style_space_around_tilde" ## [14] "spacing_around_op" ## [15] "spacing_around_comma" ## [16] "remove_space_after_opening_paren" ## [17] "remove_space_after_excl" ## [18] "set_space_after_bang_bang" ## [19] "remove_space_before_dollar" ## [20] "remove_space_after_fun_dec" ## [21] "remove_space_around_colons" ## [22] "start_comments_with_space" ## [23] "remove_space_after_unary_pm_nested" ## [24] "spacing_before_comments" ## [25] "set_space_between_levels" ## [26] "set_space_between_eq_sub_and_comma" ## ## $token ## [1] "force_assignment_op" ## [2] "resolve_semicolon" ## [3] "add_brackets_in_pipe" ## [4] "remove_terminal_token_before_and_after" ## [5] "wrap_if_else_multi_line_in_curly" ## ## $indention ## [1] "update_indention_ref_fun_dec" ## ## $use_raw_indention ## NULL ## ## $reindention ## [1] "indention" "comments_only"
Spotted the rule we want to get rid of? It’s under token
and it’s called
force_assignment_op
. I agree, we could have chosen a better name. If you are
not sure if you can guess from the name of the rule what it does you can also
have a look at the function declaration of this (unexported) function.
styler:::force_assignment_op ## function (pd) ## { ## to_replace <- pd$token == "EQ_ASSIGN" ## pd$token[to_replace] <- "LEFT_ASSIGN" ## pd$text[to_replace] <- "<-" ## pd ## } ## <bytecode: 0x3e00828> ## <environment: namespace:styler>
Next, you simply set that element to NULL
.
transformers$token$force_assignment_op <- NULL
And you can use the modified transformer list as input to style_text()
style_text("string = 'hi there'", transformers = transformers) string = "hi there"
That’s it. Note that the transformer functions and how they are returned by
tidyverse_style()
is not part of the exposed API. This means that the order,
the naming etc. may change. For example, I only recently spotted that the rule
to remove quotes (fix_quotes
)is in the category space, which is clearly
wrong and I think I will move it over to token in a future release of styler.
Some other rules and their tranformers
- You don’t like multi-line ifelse statements getting wrapped around curly
braces:
transformers$token$wrap_if_else_multi_line_in_curly
. - You don’t like mutli-line calls to be broken before the first named argument:
transformers$line_break$set_line_break_after_opening_if_call_is_multi_line
(interacting withtransformers$line_break$set_line_break_before_closing_call
). - You don’t like the line being broken after the pipe:
transformers$line_break$add_line_break_after_pipe
- You don’t like single quotes to be replaced by double quotes:
transformers$space$fix_quotes
. - You don’t like comments to start with one space:
transformers$space$start_comments_with_space
I think you get the idea. I nevertheless recommend using the tidyverse style guide as is since
- it is a well-established, thought-through style.
- using a consistent style (no matter which) reduces fraction in the community.
In case you want to add a custom rule, the vignette Customizing styler is still the way to go. If you have questions, don’t hesitate to post on Stackoverflow or leave a comment below.
- One example is
math_token_spacing
. It requires an input that is typically easiest created with another function, e.g. specify_math_token_spacing() [return]
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.