styler 1.2.0

[This article was first published on Posts on Lorenz Walthert, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

We are pleased to announce that styler 1.2.0 is now available on CRAN. All the below features were added after styler 1.1.0, except the ones listed under Other changes were added somewhere between 1.0.0 and 1.1.0. Let’s get started:

install.packages("styler")
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.1

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

There are two new way of using styler:

  • 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.

  • Via 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.

Acknowledgements

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.


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

To leave a comment for the author, please follow the link and comment on their blog: Posts on Lorenz Walthert.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)