You can now install it from CRAN
If your CRAN mirror does not yet have it, you can get it from GitHub with
The package formats R code, by default according to the tidyverse style guide. The distinguishing feature of styler is its flexibility. We will introduce some of the options below. Before I continue, I want to thank my two mentors from Google Summer of Code, in particular Kirill Müller, who was an amazing companion during the three months of coding - and beyond. I feel really blessed how everything came about. In addition, I would like to thank Google for organizing GSOC this year and facilitating the involvement of students in open source projects.
Back to the package:
styler can style text, single files, packages and entire
R source trees with the following functions:
style_text()styles a character vector.
style_file()styles R and Rmd files.
style_dir()styles all R and/or Rmd files in a directory.
style_pkg()styles the source files of an R package.
- An RStudio Addin that styles the active file R or Rmd file, the current package or the highlighted code.
We can limit ourselves to styling just spacing information by indicating this
If you are reading this post on r-bloggers, there might be issues with displaying code and rendered output correctly. You can continue reading on the page this post was published initially.
Or, on the other extreme of the scale, styling spaces, indention, line breaks and tokens:
Another option that is helpful to determine the level of ‘invasiveness’ is
strict. If set to
TRUE, spaces and line breaks before or after tokens are
set to either zero or one. However, in some situations this might be
undesirable (so we set
strict = FALSE), as the following example shows:
We prefer to keep the equal sign after “small”, “medium” and large aligned,
so we set
strict = FALSE to set spacing to at least one around
Though simple, hopefully the above examples convey some of the flexibility of
the configuration options available in
styler. You can find out more about
options available with the tidyverse style by checking out the help file for
In the sequel, let us focus on a configuration with
strict = TRUE and
scope = "tokens" and illustrate a few more examples of
code before and after styling.
styler can identify and handle unary operators and other math tokens:
This is tidyverse style. However, styler offers very granular control for
math token spacing. Assuming you like spacing around
-, but not
^. This can be achieved as follows:
It can also format complicated expressions that involve line breaking and indention based on both brace expressions and operators:
Lines are broken after
( if a function call spans multiple lines:
<- for assignment, handles single quotes within
strings if necessary, and adds braces to function calls in pipes:
Function declarations are indented if multi-line:
styler can also deal with tidyeval syntax:
If you, say, don’t want comments starting with
### to be indented, you can
formulate an unindention rule:
Customizing styler - implementing your own style guide
Not only can you customize styler with the options of
real flexibility of
styler is supporting third-party style
guides. Technically speaking, a style guide such as
nothing but a set of transformer functions and options. How you can create
your own style guide is explained in this
I hope I have convinced you that you should give
styler a try. If you find
unexpected behavior, you are welcome to file an issue on