Non-Invasive Pretty Printing of R Code

Pretty-prints R code without changing the user's formatting intent.


Build Status AppVeyor Build Status Project Status: Active – The project has reached a stable, usable state and is being actively developed. codecov cran version

The goal of styler is to provide non-invasive pretty-printing of R source code while adhering to the tidyverse formatting rules. Support for custom style guides is planned.

You can install the package from GitHub:

remotes::install_github("krlmlr/styler")

You can style a simple character vector of code with style_text():

ugly_code <- "a<-function( x){1+1}           "
style_text(ugly_code)
#> a <- function(x) {
#>   1 + 1
#> }

There are a few variants of style_text():

  • style_file() styles .R and/or .Rmd files.
  • style_dir() styles all .R files in a directory.
  • style_pkg() styles the source files of an R package.
  • RStudio Addins for styling the active file, styling the current package and styling the highlighted code region.

You can find more information on the wiki of Google Summer of Code 2017 or check out the pkgdown page.

News

styler 1.0.0 (2017-12-05)

Initial release.

stylers

These are functions used to style code. They style a directory, a whole package, a file or a string.

style_dir(path = ".", 
  ..., style = tidyverse_style, transformers = style(...), 
  filetype = "R", recursive = TRUE, exclude_files = NULL
)

style_pkg(pkg = ".", 
  ..., style = tidyverse_style, transformers = style(...), filetype = "R", 
  exclude_files = "R/RcppExports.R"
)


style_file(path, 
  ..., style = tidyverse_style, transformers = style(...)
)

style_text(text, ..., style = tidyverse_style, transformers = style(...))

style guides

These functions are the style guides implemented.

tidyverse_style(
  scope = "tokens", 
  strict = TRUE, 
  indent_by = 2, 
  start_comments_with_one_space = FALSE, 
  reindention = tidyverse_reindention(), 
  math_token_spacing = tidyverse_math_token_spacing()
)
tidyverse_reindention()
tidyverse_math_token_spacing())

style guide creators

This function is used to create a style guide.

create_style_guide(
  initialize = default_style_guide_attributes, 
  line_break = NULL, 
  space = NULL, 
  token = NULL, 
  indention = NULL, 
  use_raw_indention = FALSE, 
  reindention = tidyverse_reindention()
)

Helpers

These are helper functions used to specify the style guides in use.

specify_math_token_spacing(
  zero = NULL, 
  one = c("'+'", "'-'", "'*'", "'/'", "'^'")
)

specify_reindention(
  regex_pattern = NULL, 
  indention = 0, 
  comments_only = TRUE
)
initialize_default_attributes(pd_flat)

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("styler")

1.0.1 by Lorenz Walthert, 2 months ago


https://github.com/r-lib/styler, https://r-lib.github.io/styler/


Report a bug at https://github.com/r-lib/styler/issues


Browse source code at https://github.com/cran/styler


Authors: Kirill Müller [aut], Lorenz Walthert [cre, aut]


Documentation:   PDF Manual  


GPL-3 license


Imports backports, cli, enc, magrittr, purrr, rematch2, rlang, rprojroot, tibble, withr

Suggests data.tree, dplyr, here, knitr, mockr, rmarkdown, rstudioapi, testthat


Imported by exampletestr, languageserver, sealr, usethis.

Suggested by pmatch, shinydashboardPlus.


See at CRAN