Learn R Programming

⚠️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("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 a single .R file.
  • 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

0.0-8

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Lorenz Walthert

Last Published

April 8th, 2024

Functions in styler (0.0-8)

combine_children

Combine child and internal child
complete_styled_expr

Complete styled expression with unstyled fraction on start / end line
add_spaces_or_newlines

Concentrate newlines or spaces in a string
add_token_terminal

Add information about previous / next token to each terminal
construct_out

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

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

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

Flatten one level of nesting with its child
create_node_from_nested_root

Convert a nested tibble into a node tree
create_style_guide

Create a style guide
enrich_terminals

Enrich flattened parse table
extract_neighbourhood

Extract text around the highlighted text
prettify_any

Prettify R code in current working directory
prettify_one

Prettify one R file
set_space_between_eq_sub_and_comma

Set space between EQ_SUB and "','"
set_space_between_levels

Set space between levels of nesting
style_guides

Style guides
rep_char

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

Serialize flattened parse data
set_multi_line

Set the multi-line column
apply_transformers

Apply transformers to a parse table
bind_with_child

Bind a parse table with one of its children
compute_indent_indices

Compute the indices that need indention
apply_ref_indention

Apply reference indention to tokens
apply_ref_indention_one

Applying reference indention of a target token
context_to_terminals

Propagate context to terminals
context_towards_terminals

Update the a parse table given outer context
create_tree

Create a tree from text
set_line_break_if_call_is_multi_line

Set line break for multi-line function calls
character_to_ordered

Convert a character vector to an ordered factor
choose_indention

Choose the indention method for the tokens
create_filler

Enrich parse table with space and line break information
create_node_from_nested

Create node from nested parse data
flatten_pd

Flatten a parse table
enhance_mapping_special

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

Parse, transform and serialize text
pd_is

Check whether a parse table corresponds to a a certain expression
set_spaces

Helper for setting spaces
set_unindention_child

Unindent a child if necessary
compute_parse_data_nested

Obtain a nested parse table from a character vector
extract_terminals

Extract terminal tokens
find_active_context

needs_indention_one

Check whether indention is needed
nest_parse_data

Nest a flat parse table
style_dir

Prettify arbitrary R code
style_file

Style a file
tidyverse_style

The tidyverse style
token_is_multi_line

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

Prettify R source code
update_indention_ref

Update the indention reference
update_newlines

Update the newlines attribute
start_comments_with_space

Start comments with a space
start_on_line

Get the start right
test_transformer

Transforming test input with a transformer function
style_region

Style a region of text given context
style_text

Style a string
unindent_child

Unindent a child
update_indention

Update indention information of parse data
testthat_file

Create the path to a test that file
tokenize

Obtain token table from text
transform_and_check

Transform a file an check the result
lookup_new_special

lookup which new tokens were created from "SPECIAL"
make_transformer

Closure to return a transformer function
needs_indention

Check whether indention is needed
style_active_file

Style the active file
visit

Visit'em all
visit_one

Transform a flat parse table with a list of transformers
style_active_region

Style the highlighted region
styler-package

Non-invasive pretty printing of R code
test_collection

Run a collection of tests
transform_file

Transform a file and output a customized message
transform_files

Transform files with transformer functions
construct_vertical

Construct an object of class vertical
initialize_attributes

Enrich parse table with space and line break information
calls_sys

Invoke a system command
create_parent_id

Create the parent id for a new token
create_pos_ids

Create valid pos_ids if possible
wrap_expr_in_curly

Wrap an expression in curly braces
find_parent

Find the parent of a nest
pd_is_multi_line

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

Wrap if-else statement in curly braces
create_tokens

Create a terminal token
find_start_line

Get the start right
validate_new_pos_ids

Validate sequence of new position ids
assert_tokens

Check token validity
lookup_tokens

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

Specify spacing around math tokens
regex_for_reindention

Return regex patterns for re-indention
reindention

Specify what is re-indented how
style_space_around_math_token

Style spacing around math tokens
style_space_around_math_token_one

Set spacing of token to a certain level
set_regex_indention

Set indention of tokens that match regex
add_id_and_short

Add column pos_id and short
get_parse_data

Obtain robust parse data
verify_str_txt

Verify the text of strings
assert_text

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

Find legit starting value for a new positional id
identify_chunks

Identify chunks within Rmd contents
get_knitr_pattern

Get chunk pattern
transform_rmd

Transform Rmd contents
transform_code

Transform code from R or Rmd files