styler

R 代码的非侵入式漂亮打印。(Non-invasive pretty printing of R code)

  • 所有者: r-lib/styler
  • 平台: Linux, Mac, Windows
  • 许可证: Other
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

styler

Build
Status
AppVeyor Build
Status
lifecycle
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. styler can be
customized to format code according to other style guides too.

Installation

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")

API

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

library("styler")
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 and/or .Rmd 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.

Functionality of styler

scope

You can decide on the level of invasiveness with the scope argument. You
can style:

  • just spaces.
  • spaces and indention.
  • spaces, indention and line breaks.
  • spaces, indention, line breaks and tokens.
ugly_code <- "a=function( x){1+1}           "
style_text(ugly_code, scope = "spaces")
#> a = function(x) {1 + 1}

Note that compared to the default used above scope = "tokens":

  • no line breaks were added.
  • <- was not replaced with =.

While spaces still got styled (around = in (x)).

strict

If you wish to keep alignment as is, you can use strict = FALSE:

style_text(
  c(
    "first  <- 4",
    "second <- 1+1"
  ),
  strict = FALSE
)
#> first  <- 4
#> second <- 1 + 1

This was just the tip of the iceberg. Learn more about customization
with the tidyverse style guide in in this
vignette. If
this is not flexible enough for you, you can implement your own style
guide, as explained in the corresponding
vignette.

Adaption of styler

styler functionality is made available through other packages, most
notably

  • usethis::use_tidy_style() styles your project according to the
    tidyverse style guide.
  • reprex::reprex(style = TRUE) to prettify reprex code before
    printing. To permanently use style = TRUE without specifying it
    every time, you can add the following line to your .Rprofile (via
    usethis::edit_r_profile()): options(reprex.styler = TRUE).
  • you can pretty-print your R code in RMarkdown reports without having
    styler modifying the source. This feature is implemented as a code
    chunk option in knitr. use tidy = "styler" in the header of a code
    chunks (e.g. ```{r name-of-the-chunk, tidy = "styler"}), or
    knitr::opts_chunk$set(tidy = "styler") at the top of your
    RMarkdown script.
  • as a pre-commit hook style-files in
    https://github.com/lorenzwalthert/pre-commit-hooks.
  • pretty-printing of drake
    workflow data frames with drake::drake_plan_source().
  • Adding styler as a fixer to the ale
    Plug-in

    for VIM.

Further resources

  • The official web documentation of
    styler, containing various vignettes function documentation as well
    as a change-log.
  • Blog
    post

    about how you can customize styler without being an expert.
  • A tidyverse.org blog
    post

    introducing the functionality of styler.
  • The wiki of Google Summer of Code
    2017

    or the pkgdown page contain
    information related to the initial development phase during Google
    Summer of Code 2017.

概览

名称与所有者r-lib/styler
主编程语言R
编程语言R (语言数: 5)
平台Linux, Mac, Windows
许可证Other
发布数112
最新版本名称v1.10.3 (发布于 )
第一版名称v0.0-1 (发布于 2017-06-15 13:45:40)
创建于2017-02-08 19:16:37
推送于2024-05-15 19:46:28
最后一次提交
星数691
关注者数13
派生数71
提交数3.4k
已启用问题?
问题数586
打开的问题数51
拉请求数569
打开的拉请求数13
关闭的拉请求数43
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?
去到顶部