Package styler. December 11, Title Non-Invasive Pretty Printing of R Code Version 1.0.0

Similar documents
Package rprojroot. January 3, Title Finding Files in Project Subdirectories Version 1.3-2

Package knitrprogressbar

Package messaging. May 27, 2018

Package cattonum. R topics documented: May 2, Type Package Version Title Encode Categorical Features

Package canvasxpress

Package sessioninfo. June 21, 2017

Package datasets.load

Package fastqcr. April 11, 2017

Package docxtools. July 6, 2018

Package fastdummies. January 8, 2018

Package liftr. R topics documented: May 14, Type Package

Package goodpractice

Package datapasta. January 24, 2018

Package tidyimpute. March 5, 2018

Package pdfsearch. July 10, 2018

Package editdata. October 7, 2017

Package robotstxt. November 12, 2017

Package spelling. December 18, 2017

Package cli. November 5, 2017

Package tidyselect. October 11, 2018

Package ECctmc. May 1, 2018

Package rcmdcheck. R topics documented: November 10, 2018

Package pkgbuild. October 16, 2018

Package clipr. June 23, 2018

Package wrswor. R topics documented: February 2, Type Package

Package spark. July 21, 2017

Package librarian. R topics documented:

Package keyholder. May 19, 2018

Package pkgload. July 8, 2018

Package dkanr. July 12, 2018

Package calpassapi. August 25, 2018

Package pmatch. October 19, 2018

Package covr. October 18, 2018

Package tibble. August 22, 2017

Package qualmap. R topics documented: September 12, Type Package

Package textrecipes. December 18, 2018

Package available. November 17, 2017

Package slickr. March 6, 2018

Package validara. October 19, 2017

Package jstree. October 24, 2017

Package SEMrushR. November 3, 2018

Package facerec. May 14, 2018

Package cloudml. June 12, 2018

Package gggenes. R topics documented: November 7, Title Draw Gene Arrow Maps in 'ggplot2' Version 0.3.2

Package modules. July 22, 2017

Package readxl. April 18, 2017

Package WordR. September 7, 2017

Package desc. May 1, 2018

Package testit. R topics documented: June 14, 2018

Package BiocManager. November 13, 2018

Package RPostgres. April 6, 2018

Package githubinstall

Package sfdct. August 29, 2017

Package barcoder. October 26, 2018

Package snakecase. R topics documented: March 25, Version Date Title Convert Strings into any Case

Package zip. R topics documented: March 11, Title Cross-Platform 'zip' Compression Version 2.0.1

Package geojsonsf. R topics documented: January 11, Type Package Title GeoJSON to Simple Feature Converter Version 1.3.

Package oec. R topics documented: May 11, Type Package

Package reval. May 26, 2015

Package internetarchive

Package repec. August 31, 2018

Package jpmesh. December 4, 2017

Package crossword.r. January 19, 2018

Package opencage. January 16, 2018

Package redux. May 31, 2018

Package shinyhelper. June 21, 2018

Package tfruns. February 18, 2018

Package gifti. February 1, 2018

Package ruler. February 23, 2018

Package comparedf. February 11, 2019

Package ezknitr. September 16, 2016

Package SNPediaR. April 9, 2019

Package workflowr. July 6, 2018

Package ggimage. R topics documented: December 5, Title Use Image in 'ggplot2' Version 0.1.0

Package jdx. R topics documented: January 9, Type Package Title 'Java' Data Exchange for 'R' and 'rjava'

Package tidytransit. March 4, 2019

Package vip. June 15, 2018

Package condformat. October 19, 2017

Package ggimage. R topics documented: November 1, Title Use Image in 'ggplot2' Version 0.0.7

Package bisect. April 16, 2018

Package radix. September 17, 2018

Package lumberjack. R topics documented: July 20, 2018

Package dotcall64. January 11, 2018

Package rgho. R topics documented: January 18, 2017

Package postgistools

Package gtrendsr. October 19, 2017

Package loggit. April 9, 2018

Package rngtools. May 15, 2018

Package gtrendsr. August 4, 2018

Package utf8. May 24, 2018

Package shinyfeedback

Package tiler. June 9, 2018

Package rtext. January 23, 2019

Package RPostgres. December 6, 2017

Package bigqueryr. October 23, 2017

Package semver. January 6, 2017

Package glue. March 12, 2019

Package wikitaxa. December 21, 2017

Package rsppfp. November 20, 2018

Package embed. November 19, 2018

Transcription:

Title Non-Invasive Pretty Printing of R Code Version 1.0.0 Package styler December 11, 2017 Pretty-prints R code without changing the user's formatting intent. Imports backports, cli, enc, magrittr, purrr, rematch2, rlang, rprojroot, tibble, withr Suggests data.tree, dplyr, here, knitr, mockr, rmarkdown, rstudioapi, testthat License GPL-3 Encoding UTF-8 LazyData true Date 2017-12-05 BugReports https://github.com/r-lib/styler/issues URL https://github.com/r-lib/styler, https://r-lib.github.io/styler/ RoxygenNote 6.0.1 VignetteBuilder knitr Collate 'addins.r' 'communicate.r' 'compat-tidyr.r' 'dplyr.r' 'expr-is.r' 'indent.r' 'initialize.r' 'nest.r' 'nested_to_tree.r' 'parse.r' 'reindent.r' 'token-define.r' 'relevel.r' 'rules-line_break.r' 'rules-other.r' 'rules-replacement.r' 'rules-spacing.r' 'serialize.r' 'serialized_tests.r' 'set-assert-args.r' 'style_guides.r' 'styler.r' 'token-create.r' 'transform-code.r' 'transform-files.r' 'ui.r' 'unindent.r' 'utils.r' 'vertical.r' 'visit.r' 'zzz.r' NeedsCompilation no Author Kirill Müller [aut], Lorenz Walthert [cre, aut] Maintainer Lorenz Walthert <lorenz.walthert@icloud.com> Repository CRAN Date/Publication 2017-12-11 12:52:26 UTC 1

2 styler-package R topics documented: styler-package........................................ 2 construct_vertical...................................... 3 create_style_guide..................................... 3 default_style_guide_attributes............................... 4 math_token_spacing.................................... 5 reindention......................................... 6 styler_addins........................................ 7 style_dir........................................... 7 style_file........................................... 8 style_guides......................................... 9 style_pkg.......................................... 10 style_text.......................................... 11 tidyverse_style....................................... 12 Index 14 styler-package Non-invasive pretty printing of R code styler allows you to format.r files, packages or entire R source trees according to a style guide. The following functions can be used for styling: Author(s) style_text() to style a character vector. style_file() to style a single.r file. style_dir() to style all.r files in a directory. style_pkg() to style the source files of an R package. styler_addins (RStudio Addins) to style either selected code or the active file. Maintainer: Lorenz Walthert <lorenz.walthert@icloud.com> Authors: See Also Kirill Müller <krlmlr+r@mailbox.org> Useful links: https://github.com/r-lib/styler https://r-lib.github.io/styler/ Report bugs at https://github.com/r-lib/styler/issues

construct_vertical 3 style_text("call( 1)") style_text("1 + 1", strict = FALSE) style_text("a%>%b", scope = "spaces") style_text("a%>%b; a", scope = "line_breaks") style_text("a%>%b; a", scope = "tokens") construct_vertical Construct an object of class vertical Sole purpose of the class vertical is to have a print method that aligns the output vertically. construct_vertical(x) x A character vector or an object of class "vertical". create_style_guide Create a style guide This is a helper function to create a style guide, which is technically speaking a named list of groups of transformer functions where each transformer function corresponds to one styling rule. The output of this function can be used as an argument for style in top level functions like style_text() and friends. create_style_guide(initialize = default_style_guide_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention())

4 default_style_guide_attributes initialize line_break space token The bare name of a function that initializes various variables on each level of nesting. A list of transformer functions that manipulate line_break information. A list of transformer functions that manipulate spacing information. A list of transformer functions that manipulate token text. indention A list of transformer functions that manipulate indention. use_raw_indention Boolean indicating whether or not the raw indention should be used. reindention A list of parameters for regex re-indention, most conveniently constructed using specify_reindention(). set_line_break_before_curly_opening <- function(pd_flat) { op <- pd_flat$token %in% "'{'" pd_flat$lag_newlines[op] <- 1L pd_flat } set_line_break_before_curly_opening_style <- function() { create_style_guide(line_break = set_line_break_before_curly_opening) } style_text("a <- function(x) { x }", style = set_line_break_before_curly_opening_style) default_style_guide_attributes Initialize default style guide attributes This function initialises and removes various variables from the parse table. default_style_guide_attributes(pd_flat) pd_flat A parse table. string_to_format <- "call( 3)" pd <- styler:::compute_parse_data_nested(string_to_format) styler:::pre_visit(pd, c(default_style_guide_attributes))

math_token_spacing 5 math_token_spacing Specify spacing around math tokens Helper function to create the input for the argument math_token_spacing in tidyverse_style(). specify_math_token_spacing(zero = NULL, one = c("'+'", "'-'", "'*'", "'/'", "'^'")) tidyverse_math_token_spacing() zero one Character vector of tokens that should be surrounded with zero spaces. Character vector with tokens that should be surrounded by at least one space (depending on strict = TRUE in the styling functions style_text() and friends). See. Functions specify_math_token_spacing: Allows to fully specify the math token spacing. tidyverse_math_token_spacing: Simple forwarder to specify_math_token_spacing with spacing around math tokens according to the tidyverse style guide. style_text( "1+1-3", math_token_spacing = specify_math_token_spacing(zero = "'+'"), strict = FALSE ) style_text( "1+1-3", math_token_spacing = specify_math_token_spacing(zero = "'+'"), strict = TRUE ) style_text( "1+1-3", math_token_spacing = tidyverse_math_token_spacing(), strict = TRUE ) style_text( "1+1-3", math_token_spacing = tidyverse_math_token_spacing(), strict = TRUE )

6 reindention reindention Specify what is re-indented how This function returns a list that can be used as an input for the argument reindention of the function tidyverse_style(). It features sensible defaults, so the user can specify deviations from them conveniently without the need of setting all arguments explicitly. specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE) tidyverse_reindention() regex_pattern indention comments_only Character vector with regular expression patterns that are to be re-indented with spaces, NULL if no reindention needed. The indention tokens should have if they match regex_pattern. Whether the regex_reindention_pattern should only be matched against comments or against all tokens. Mainly added for performance. Functions specify_reindention: Allows to specify which tokens are reindented and how. tidyverse_reindention: Simple forwarder to specify_reindention with reindention according to the tidyverse style guide. style_text("a <- xyz", reindention = specify_reindention( regex_pattern = "xyz", indention = 4, comments_only = FALSE) ) style_text("a <- xyz", reindention = tidyverse_reindention())

styler_addins 7 styler_addins Stylers for RStudio Addins Helper functions for styling via RStudio Addins. style_active_file() style_selection() Functions style_active_file: Styles the active file with tidyverse_style() and strict = TRUE. style_selection: Styles the highlighted selection in a.r or.rmd file. Auto-Save Option By default, both of the RStudio Addins will apply styling to the (selected) file contents without saving changes. Automatic saving can be enabled by setting the environment variable save_after_styling to TRUE. Consider setting this in your.rprofile file if you want to persist this setting across multiple sessions. Untitled files will always need to be saved manually after styling. See Also Sys.setenv() Other stylers: style_dir, style_file, style_pkg, style_text style_dir Prettify arbitrary R code Performs various substitutions in all.r files in a directory. Carefully examine the results after running this function! style_dir(path = ".",..., style = tidyverse_style, transformers = style(...), filetype = "R", recursive = TRUE, exclude_files = NULL)

8 style_file Value path Path to a directory with files to transform.... passed on to the style function. style transformers filetype recursive exclude_files A function that creates a style guide to use, by default tidyverse_style() (without the parentheses). Not used further except to construct the argument transformers. See style_guides() for details. A set of transformer functions. This argument is most conveniently constructed via the style argument and... See. Vector of file extensions indicating which filetypes should be styled. Case is ignored, and the. is optional, e.g. c(".r", ".Rmd") or c("r", "rmd"). A logical value indicating whether or not files in subdirectories of path should be styled as well. Character vector with paths to files that should be excluded from styling. Invisibly returns a data frame that indicates for each file considered for styling whether or not it was actually changed. Warning This function overwrites files (if styling results in a change of the code to be formatted). It is strongly suggested to only style files that are under version control or to create a backup copy. See Also Other stylers: style_file, style_pkg, style_text, styler_addins ## Not run: style_dir(file_type = "r") ## End(Not run) style_file Style.R and/or.rmd files Performs various substitutions in the files specified. Carefully examine the results after running this function! style_file(path,..., style = tidyverse_style, transformers = style(...))

style_guides 9 path A character vector with paths to files to style.... passed on to the style function. style transformers A function that creates a style guide to use, by default tidyverse_style() (without the parentheses). Not used further except to construct the argument transformers. See style_guides() for details. A set of transformer functions. This argument is most conveniently constructed via the style argument and... See. Value Invisibly returns a data frame that indicates for each file considered for styling whether or not it was actually changed. Warning This function overwrites files (if styling results in a change of the code to be formatted). It is strongly suggested to only style files that are under version control or to create a backup copy. See Also Other stylers: style_dir, style_pkg, style_text, styler_addins # the following is identical but the former is more convenient: file <- tempfile("styler", fileext = ".R") enc::write_lines_enc("1++1", file) style_file(file, style = tidyverse_style, strict = TRUE) style_file(file, transformers = tidyverse_style(strict = TRUE)) enc::read_lines_enc(file) unlink(file) style_guides Style guides Format code according to a style guide. Style guides are the input to the argument style in style_file() and friends. The available style guides are: the tidyverse style guide (see tidyverse_style()).

10 style_pkg style_pkg Prettify R source code Performs various substitutions in all.r files in a package (code and tests). Carefully examine the results after running this function! style_pkg(pkg = ".",..., style = tidyverse_style, transformers = style(...), filetype = "R", exclude_files = "R/RcppExports.R") pkg Path to a (subdirectory of an) R package.... passed on to the style function. style transformers filetype exclude_files A function that creates a style guide to use, by default tidyverse_style() (without the parentheses). Not used further except to construct the argument transformers. See style_guides() for details. A set of transformer functions. This argument is most conveniently constructed via the style argument and... See. Vector of file extensions indicating which filetypes should be styled. Case is ignored, and the. is optional, e.g. c(".r", ".Rmd") or c("r", "rmd"). Character vector with paths to files that should be excluded from styling. Warning This function overwrites files (if styling results in a change of the code to be formatted). It is strongly suggested to only style files that are under version control or to create a backup copy. Value Invisibly returns a data frame that indicates for each file considered for styling whether or not it was actually changed. See Also Other stylers: style_dir, style_file, style_text, styler_addins

style_text 11 ## Not run: style_pkg(style = tidyverse_style, strict = TRUE) style_pkg( scope = "line_breaks", math_token_spacing = specify_math_token_spacing(zero = "'+'") ) ## End(Not run) style_text Style a string Styles a character vector. Each element of the character vector corresponds to one line of code. style_text(text,..., style = tidyverse_style, transformers = style(...)) text A character vector with text to style.... passed on to the style function. style See Also transformers A function that creates a style guide to use, by default tidyverse_style() (without the parentheses). Not used further except to construct the argument transformers. See style_guides() for details. A set of transformer functions. This argument is most conveniently constructed via the style argument and... See. Other stylers: style_dir, style_file, style_pkg, styler_addins style_text("call( 1)") style_text("1 + 1", strict = FALSE) style_text("a%>%b", scope = "spaces") style_text("a%>%b; a", scope = "line_breaks") style_text("a%>%b; a", scope = "tokens") # the following is identical but the former is more convenient: style_text("a<-3++1", style = tidyverse_style, strict = TRUE) style_text("a<-3++1", transformers = tidyverse_style(strict = TRUE))

12 tidyverse_style tidyverse_style The tidyverse style Style code according to the tidyverse style guide. 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()) Details scope strict The extent of manipulation. Can range from "none" (least invasive) to "token" (most invasive). See Details. This argument is a vector of length one. A logical value indicating whether a set of strict or not so strict transformer functions should be returned. Compare the functions returned with or without strict = TRUE. For example, strict = TRUE means force one space e.g. after "," and one line break e.g. after a closing curly brace. strict = FALSE means to set spaces and line breaks to one if there is none and leave the code untouched otherwise. See. indent_by How many spaces of indention should be inserted after operators such as (. start_comments_with_one_space Whether or not comments should start with only one space (see start_comments_with_space()). reindention A list of parameters for regex re-indention, most conveniently constructed using specify_reindention(). math_token_spacing A list of parameters that define spacing around math token, conveniently constructed using specify_math_token_spacing(). The following options for scope are available. "none": Performs no transformation at all. "spaces": Manipulates spacing between token on the same line. "indention": In addition to "spaces", this option also manipulates the indention level. "line_breaks": In addition to "indention", this option also manipulates line breaks. "tokens": In addition to "line_breaks", this option also manipulates tokens. As it becomes clear from this description, more invasive operations can only be performed if all less invasive operations are performed too.

tidyverse_style 13 style_text("call( 1)", style = tidyverse_style, scope = "spaces") style_text("call( 1)", transformers = tidyverse_style(strict = TRUE)) style_text(c("ab <- 3", "a <-3"), strict = FALSE) # keeps alignment of "<-" style_text(c("ab <- 3", "a <-3"), strict = TRUE) # drops alignment of "<-"

Index construct_vertical, 3 create_style_guide, 3 default_style_guide_attributes, 4 math_token_spacing, 5 reindention, 6 specify_math_token_spacing (math_token_spacing), 5 specify_math_token_spacing(), 12 specify_reindention (reindention), 6 specify_reindention(), 4, 12 style_active_file (styler_addins), 7 style_dir, 7, 7, 9 11 style_dir(), 2 style_file, 7, 8, 8, 10, 11 style_file(), 2, 9 style_guides, 9 style_guides(), 8 11 style_pkg, 7 9, 10, 11 style_pkg(), 2 style_selection (styler_addins), 7 style_text, 7 10, 11 style_text(), 2, 3, 5 styler (styler-package), 2 styler-package, 2 styler_addins, 2, 7, 8 11 Sys.setenv(), 7 tidyverse_math_token_spacing (math_token_spacing), 5 tidyverse_reindention (reindention), 6 tidyverse_style, 12 tidyverse_style(), 5 11 14