Unlimited learning, half price | 50% off

Last chance! 50% off unlimited learning

Sale ends in


⚠️There's a newer version (1.10.3) 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 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

46,574

Version

1.0.9000

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Lorenz Walthert

Last Published

April 8th, 2024

Functions in styler (1.0.9000)

add_spaces_or_newlines

Concentrate newlines or spaces in a string
add_line_col_to_wrapped_expr

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

Add information about previous / next token to each terminal
apply_transformers

Apply transformers to a parse table
add_id_and_short

Add column pos_id and short
assert_tokens

Check token validity
assert_filetype

Make sure all supplied file types are allowed
compute_indent_indices

Compute the indices that need indention
assert_text

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

Obtain a nested parse table from a character vector
can_verify_roundtrip

Check whether a roundtrip verification can be carried out
apply_ref_indention

Apply reference indention to tokens
character_to_ordered

Convert a character vector to an ordered factor
apply_ref_indention_one

Applying reference indention of a target token
context_to_terminals

Propagate context to terminals
communicate_summary

Communicate the summary of styling
context_towards_terminals

Update the a parse table given outer context
create_tree

Create a tree from text
communicate_warning

Communicate a warning if necessary
default_style_guide_attributes

Initialize default style guide attributes
copy_to_tempdir

Copy a file to a temporary directory
create_node_from_nested_root

Convert a nested tibble into a node tree
create_pos_ids

Create valid pos_ids if possible
create_node_from_nested

Create node from nested parse data
extract_terminals

Extract terminal tokens
choose_indention

Choose the indention method for the tokens
find_block_id

Find the block to which a token belongs
get_knitr_pattern

Get chunk pattern
combine_children

Combine child and internal child
get_parse_data

Obtain robust parse data
enhance_mapping_special

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

Bind a parse table with one of its children
calls_sys

Invoke a system command
enrich_terminals

Enrich flattened parse table
construct_out

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

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

Identify chunks within Rmd contents
initialize_attributes

Initialize attributes
expressions_are_identical

Check whether two expressions are identical
make_transformer

Closure to return a transformer function
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.
map_filetype_to_pattern

Map the file type to a corresponding regular expression
flatten_pd

Flatten a parse table
relocate_eq_assign

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

Relocate all assignment expressions that contain EQ_ASSIGN within a nest
construct_vertical

Construct an object of class vertical
start_comments_with_space

Start comments with a space
contains_else_expr_that_needs_braces

Check whether an else expression needs braces
style_dir

Prettify arbitrary R code
create_style_guide

Create a style guide
testthat_file

Create the path to a test that file
tidyverse_style

The tidyverse style
verify_str_txt

Verify the text of strings
verify_roundtrip

Verify the styling
next_non_comment

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

Check whether indention is needed
nest_parse_data

Nest a flat parse table
parse_transform_serialize

Parse, transform and serialize text
relocate_eq_assign_one

Relocate an assignment expression
pd_is

Check whether a parse table corresponds to a certain expression
find_start_line

Get the start right
pd_is_multi_line

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

Find legit starting value for a new positional id
math_token_spacing

Specify spacing around math tokens
set_arg_write_tree

Set the write_tree argument
needs_indention

Check whether indention is needed
set_line_break_if_call_is_multi_line

Set line break for multi-line function calls
serialize_parse_data_flattened

Serialize flattened parse data
set_and_assert_arg_filetype

Set the file type argument
style_pkg

Prettify R source code
test_collection

Run a collection of tests
style_space_around_math_token

Style spacing around math tokens
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.
transform_files

Transform files with transformer functions
lookup_new_special

lookup which new tokens were created from "SPECIAL"
lookup_tokens

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

Transforming test input with a transformer function
transform_rmd

Transform Rmd contents
try_transform_as_r_file

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

Wrap if-else statement in curly braces
wrap_subexpr_in_curly

Wrap a sub-expression in curly braces
unindent_child

Unindent a child
rep_char

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

Wrap an expression in curly braces
set_spaces

Helper for setting spaces
wrap_expr_in_expr

Wrap an expression into an expression
set_unindention_child

Unindent a child if necessary
style_space_around_math_token_one

Set spacing of token to a certain level
create_tokens

Create a terminal token
style_file

Style .R and/or .Rmd files
flatten_operators

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

Style guides
tokenize

Obtain token table from text
flatten_operators_one

Flatten one level of nesting with its child
transform_and_check

Transform a file an check the result
set_space_between_eq_sub_and_comma

Set space between EQ_SUB and "','"
style_text

Style a string
set_space_between_levels

Set space between levels of nesting
update_indention

Update indention information of parse data
update_indention_ref

Update the indention reference
update_newlines

Update the newlines attribute
prettify_any

Prettify R code in current working directory
set_multi_line

Set the multi-line column
validate_new_pos_ids

Validate sequence of new position ids
set_regex_indention

Set indention of tokens that match regex
visit_one

Transform a flat parse table with a list of transformers
styler-package

Non-invasive pretty printing of R code
reindention

Specify what is re-indented how
styler_addins

Stylers for RStudio Addins
transform_code

Transform code from R or Rmd files
transform_file

Transform a file and output a customized message
visit

Visit'em all