Learn R Programming

⚠️There's a newer version (1.11.0) of this package.Take me there.

styler

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 CRAN:

install.packages("styler")

Or get the development version from GitHub:

# install.packages("remotes")
remotes::install_github("r-lib/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.

Copy Link

Version

Install

install.packages('styler')

Monthly Downloads

58,138

Version

1.0.2

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Lorenz Walthert

Last Published

June 26th, 2018

Functions in styler (1.0.2)

create_node_from_nested_root

Convert a nested tibble into a node tree
create_node_from_nested

Create node from nested parse data
construct_vertical

Construct an object of class vertical
calls_sys

Invoke a system command
set_regex_indention

Set indention of tokens that match regex
bind_with_child

Bind a parse table with one of its children
compute_parse_data_nested

Obtain a nested parse table from a character vector
context_to_terminals

Propagate context to terminals
context_towards_terminals

Update the a parse table given outer context
enhance_mapping_special

Enhance the mapping of text to the token "SPECIAL"
needs_indention_one

Check whether indention is needed
get_knitr_pattern

Get chunk pattern
prettify_any

Prettify R code in current working directory
nest_parse_data

Nest a flat parse table
enrich_terminals

Enrich flattened parse table
find_tokens_to_update

Find the tokens to update when applying a reference indention
find_start_pos_id

Find legit starting value for a new positional id
get_engine_pattern

What's the engine pattern for rmd code chunks?
pd_is_multi_line

Check whether a parse table is a multi-line token
styler_addins

Stylers for RStudio Addins
contains_else_expr_that_needs_braces

Check whether an else expression needs braces
set_arg_write_tree

Set the write_tree argument
extract_terminals

Extract terminal tokens
identify_insufficiently_parsed_stings

Identify strings that were not fully parsed
find_block_id

Find the block to which a token belongs
identify_r_raw_chunks

Identifies raw R code chunks
expressions_are_identical

Check whether two expressions are identical
find_line_break_position_in_multiline_call

Find index of the token before which the line should be broken
set_space_between_eq_sub_and_comma

Set space between EQ_SUB and "','"
relocate_eq_assign

Relocate the expressions containing the token EQ_ASSIGN within the nested parse table
reindention

Specify what is re-indented how
set_space_between_levels

Set space between levels of nesting
character_to_ordered

Convert a character vector to an ordered factor
set_line_break_if_call_is_multi_line

Set line break for multi-line function calls
style_text

Style a string
create_tree

Create a tree from text
tokenize

Obtain token table from text
style_space_around_token

Set spacing of token to a certain level
default_style_guide_attributes

Initialize default style guide attributes
extend_if_comment

Find the index of the last comment in the sequence of comments-only tokens after the token that has position pos in pd.
transform_and_check

Transform a file an check the result
transform_files

Transform files with transformer functions
styler-package

Non-invasive pretty printing of R code
transform_rmd

Transform Rmd contents
find_start_line

Get the start right
unindent_child

Unindent a child
relocate_eq_assign_nest

Relocate all assignment expressions that contain EQ_ASSIGN within a nest
create_pos_ids

Create valid pos_ids if possible
unindent_fun_dec

Revert the indention of function declaration header
create_style_guide

Create a style guide
flatten_pd

Flatten a parse table
create_tokens

Create a terminal token
generate_test_samples

Generate a comprehensive collection test cases for comment / insertion interaction Test consist of if / if-else / if-else-if-else cases, paired with various line-break and comment configurations. Used for internal testing.
ensure_correct_str_txt

Ensure a correct text of all strings
math_token_spacing

Specify spacing around math tokens
transform_code

Transform code from R or Rmd files
flatten_operators

Flatten some token in the nested parse table based on operators
wrap_expr_in_expr

Wrap an expression into an expression
is_shebang

Identify comments that are shebangs
needs_indention

Check whether indention is needed
flatten_operators_one

Flatten one level of nesting with its child
get_parse_data

Obtain robust parse data
wrap_if_else_multi_line_in_curly

Wrap if-else statement in curly braces
ensure_valid_pd

Ensure that the parse data is valid
relocate_eq_assign_one

Relocate an assignment expression
has_crlf_as_first_line_sep

Check if a string uses CRLF EOLs
initialize_attributes

Initialize attributes
is_tilde_expr

Check whether a parse table contains a tilde
make_transformer

Closure to return a transformer function
is_code_chunk_header

Identify spinning code chunk header
lookup_new_special

Lookup which new tokens were created from "SPECIAL"
set_multi_line

Set the multi-line column
map_filetype_to_pattern

Map the file type to a corresponding regular expression
verify_roundtrip

Verify the styling
next_non_comment

Find the index of the next non-comment in a parse table
rep_char

Repeat elements of a character vector times times and collapse it
set_spaces

Helper for setting spaces
parse_safely

Save parsing from text
set_unindention_child

Unindent a child if necessary
lookup_tokens

Lookup all tokens that have a unique token-text mapping
parse_transform_serialize

Parse, transform and serialize text
start_comments_with_space

Start comments with a space
style_dir

Prettify arbitrary R code
pd_is

Check whether a parse table corresponds to a certain expression
separate_chunks

Separate chunks within Rmd contents
testthat_file

Create the path to a test that file
transform_file

Transform a file and output a customized message
style_pkg

Prettify R source code
update_indention

Update indention information of parse data
tidyverse_style

The tidyverse style
style_space_around_math_token

Style spacing around math tokens
visit

Visit'em all
test_collection

Run a collection of tests
test_transformer

Transforming test input with a transformer function
two_cols_match

Check whether two columns match
try_transform_as_r_file

Style a file as if it was an .R file
visit_one

Transform a flat parse table with a list of transformers
update_indention_ref

Update the indention reference
serialize_parse_data_flattened

Serialize flattened parse data
wrap_expr_in_curly

Wrap an expression in curly braces
set_and_assert_arg_filetype

Set the file type argument
wrap_subexpr_in_curly

Wrap a sub-expression in curly braces
style_guides

Style guides
style_file

Style .R and/or .Rmd files
update_newlines

Update the newlines attribute
validate_new_pos_ids

Validate sequence of new position ids
add_spaces_or_newlines

Concentrate newlines or spaces in a string
apply_ref_indention

Apply reference indention to tokens
apply_transformers

Apply transformers to a parse table
add_line_col_to_wrapped_expr

Adds line and col information to an expression from its child
choose_indention

Choose the indention method for the tokens
add_id_and_short

Add column pos_id and short
can_verify_roundtrip

Check whether a roundtrip verification can be carried out
assert_text

Assert text to be of positive length and replace it with the empty string otherwise.
add_token_terminal

Add information about previous / next token to each terminal
combine_children

Combine child and internal child
apply_ref_indention_one

Applying reference indention of a target token
compute_indent_indices

Compute the indices that need indention
assert_filetype

Make sure all supplied file types are allowed
assert_tokens

Check token validity
construct_out

Construct *-out.R from a *-in.R
copy_to_tempdir

Copy a file to a temporary directory
construct_tree

Construct paths of a tree object given the paths of *-in.R files
communicate_summary

Communicate the summary of styling
communicate_warning

Communicate a warning if necessary