The goal of styler is to provide non-invasive pretty-printing of R source code while adhering to the tidyverse formatting rules. styler can be customized to format code according to other style guides too.
You can install the package from CRAN:
Or get the development version from GitHub:
You can style a simple character vector of code with
There are a few variants of
style_file()styles .R and/or .Rmd files.
style_dir()styles all .R and/or .Rmd files in a directory.
style_pkg()styles the source files of an R package.
You can decide on the level of invasiveness with the scope argument. You can style:
Note that compared to the default used above
scope = "tokens":
<-was not replaced with
While spaces still got styled (around
If you wish to keep alignment as is, you can use
strict = FALSE:
This was just the tip of the iceberg. Learn more about customization with the tidyverse style guide in in this vignette. If this is not flexible enough for you, you can implement your own style guide, as explained in the corresponding vignette
styler functionality is made available through other packages, most notably
usethis::use_tidy_style()styles your project according to the tidyverse style guide.
reprex::reprex(style = TRUE)to prettify reprex code before printing. To permanently use
style = TRUEwithout specifying it every time, you can add the following line to your
options(reprex.styler = TRUE).