styler 1.2.0

  r-lib, styler

  Lorenz Walthert

We are pleased to announce that styler 1.2.0 is now available on CRAN. The initial version of styler was released in December 2017. Since then, five new versions were released, and styler was improved substantially, but many improvements were related to special cases or even edge cases. These are all contained in the change log. Here, we want to focus on a few changes since version 1.0.0 that we believe are the most relevant. You can install the latest stable version from CRAN with install.packages("styler"). Let’s get started:

library(styler)

Alignment detection

styler can finally detect aligned code and keep it aligned! For example, the following code won’t be modified by styler:

call(
  some_arg = 123,
  more     = "x"
)

We’ve tried to make this as intuitive as possible, but have a look at dedicated vignette when you are dealing with more complicated calls than the one above. Note that the detection currently only works inside function calls, so styler will not recognize the below as aligned.

x  <- 2
x2 <- f(x)

and turn it into

x <- 2
x2 <- f(x)

unless you also use strict = FALSE.^[E.g. styler::style_text(..., strict = FALSE), but note that this also has other effects on styling that you might not want.]

rlang’s {{

In rlang 0.4.0 a new interpolation operator was introduced: {{ (read curly curly). Because styler < 1.2.0 understood these as regular curly braces, you ended up with

call({
  {
    x
  }
})

when styling call({{x}}), which is nonsense. Now styler yields call({{ x }}).

Addins

The Style active file addin now remembers the cursor position and the details of styling can be specified as an R option:

options(
  styler.addins_style_transformer = "styler::tidyverse_style(scope = 'spaces')"
)

You can also set the value of this option interactively with the set style addin (not persistent over sessions). For details, see help("styler_addins", "styler") and help("tidyverse_style", "styler").

The customization of the styling does not affect the command-line API (styler::style_text() and friends). We are not sure how users could best customize styling, but you can track our progress on that in r-lib/styler#319.

You can also set the environment variable save_after_styling to TRUE, if you are tired of saving the file after styling it with the addin.

Braces in function calls

tryCatch() expressions often look like this:

tryCatch(
  {
    exp(x)
  },
  error = function(x) x
)

Prior to version 1.2.0, styler would return this odd formatting:

tryCatch({
  exp(x)
},
error = function(x) x
)

Now, the line is broken before the opening curly brace in function calls, except if there is only one brace expression and it’s the last in the function call. The typical use case is testthat::test_that(...), i.e. the following code won’t be modified:

test_that("some condition holds", {
  some_code()
})

Other changes

  • styler depends on tibble >= 1.4.2 and runs 2x as fast as initially.

  • styler can style roxygen code examples in the source code of packages.

  • styler can style .Rnw files.

  • The print method for the output of style_text() returns syntax-highlighted code by default, controllable via the option styler.colored_print.vertical.

Adaption of styler

We’d like to highlight that styler integrates with various other tools you might be using:

  • As a git pre-commit hook. Two standard calls from the R console, and you are all set. We are convinced that this is the preferred way of using styler to ensure all your files are consistently formatted. Check out the precommit package that also implements many other useful hooks.

  • usethis::use_tidy_style() styles your project according to the tidyverse style guide.

  • knitr::knitr() and friends recognize the R code chunk option tidy = "styler" for .Rnw and .Rmd files to pretty-print code.

  • 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 (e.g. via usethis::edit_r_profile()): options(reprex.styler = TRUE).

  • There are plugins for Emacs and VIM.

Outlook

We have some cool new features in the pipeline such as caching for faster styling, and making styler ignore some lines, which you can try out by installing from the respective branches. Feedback welcome.

Acknowledgments

We are grateful to all of the people who contributed not just code, but also issues and comments over the last two years:

@aaronrudkin, @aedobbyn, @ArthurPERE, @Banana1530, @batpigandme, @Bio7, @ClaytonJY, @courtiol, @crew102, @cpsievert, @dchiu911, @devSJR, @dirkschumacher, @ellessenne, @Emiller88, @fny, @hadley, @Hasnep, @igordot, @IndrajeetPatil, @jackwasey, @jcrodriguez1989, @jennybc, @jjramsey, @jkgrain @jonmcalder, @joranE, @kalibera, @katrinleinweber, @kiranmaiganji, @krivit, @krlmlr, @llrs, @lorenzwalthert, @lwjohnst86, @martin-mfg, @maurolepore, @michaelquinn32, @mine-cetinkaya-rundel, @Moohan, @msberends, @NGaffney, @nxskok, @oliverbeagley, @pat-s, @ramnathv, @raynamharris, @reddy-ia, @riccardoporreca, @rillig, @rjake, @Robinlovelace, @RMHogervorst, @rorynolan, @russHyde, @samhinshaw, @skirmer, @thalesmello, @tobiasgerstenberg, @tonytonov, @tvatter, @vnijs, @wdearden, @wlandau, @wmayner, @yech1990 and @yutannihilation.