Change default indent in styler to 4 spaces vs. 2

I have tried to read through the styler customization vignette but it is beyond my grasp. Is there a relatively simple way to modify the default indenting from 2 spaces to 4 spaces? Or, an article that might help me get there?

So that:

mtcars %>%
  dplyr::group_by(am, cyl) %>%
  dplyr::summarise(
    drat_mean = mean(drat),
    disp_mean = mean(disp)
  )

becomes:

mtcars %>%
    dplyr::group_by(am, cyl) %>%
    dplyr::summarise(
        drat_mean = mean(drat),
        disp_mean = mean(disp)
    )

Not sure if this complicates it: I am hoping to be able to run this from a keyboard shortcut via Rstudio add-ins.

Luckily, you don't need to dive into serious customization :smile: — the default (and currently only existing) style guide, tidyverse_style() has an indent_by parameter.

The part I found tricky:

There are 3 different equivalent ways of calling the top-level styling functions with your own values passed to tidyverse_style() parameters, which allows for shortcuts in some circumstances but I feel like is also a lot to wrap your head around. Adapted from the Customization vignette:

library(styler)

string_to_format <-
"mtcars %>%
  dplyr::group_by(am, cyl) %>%
  dplyr::summarise(
    drat_mean = mean(drat),
    disp_mean = mean(disp)
  )"

all.equal(
  style_text(string_to_format, transformers = tidyverse_style(indent_by = 4)),
  style_text(string_to_format, style = tidyverse_style, indent_by = 4),
  style_text(string_to_format, indent_by = 4),
)
#> [1] TRUE

style_text(string_to_format, style = tidyverse_style, indent_by = 4)
#> mtcars %>%
#>     dplyr::group_by(am, cyl) %>%
#>     dplyr::summarise(
#>         drat_mean = mean(drat),
#>         disp_mean = mean(disp)
#>     )

Created on 2018-11-19 by the reprex package (v0.2.1)

The style parameter is basically just a shortcut that kicks off the construction of the transformers argument, so you can't do style = tidyverse_style(indent_by = 4).

Edited to add: I personally avoid the third syntax, because having that tidyverse_style() parameter sitting there looking like it's a style_text() parameter makes me itchy (I expect future-me-who-no-longer-remembers-how-it-all-works will not approve), but YMMV!

4 Likes

What happens if you change the number of spaces for an indent in your preferences?

** I like @jcblum's suggestion better. I was thinking of doing the reverse. Change your tab width, and then turn off the styler indent.

That was a huge help and very fast!
To finish this by making it an add-in, I created this .R for a package.

I'm sure some of it is not best practice.

# usethis::use_package("styler")
# usethis::use_package("rstudioapi")

#' style 4 spaces
#'
#' from {https://github.com/r-lib/styler/blob/0154a18943c09b99dd67e90080edaa2fe358af97/R/addins.R}
#' from {https://community.rstudio.com/t/change-default-indent-in-styler-to-4-spaces-vs-2/18474/2}
#'
#' @return
#' @export
#'
style_selection_4spaces <- function(){
    context <- get_rstudio_context()
    text <- context$selection[[1]]$text
    if (all(nchar(text) == 0)) stop("No code selected")
    out <- styler::style_text(text, style = styler::tidyverse_style(), transformers = styler::tidyverse_style(indent_by = 4))
    rstudioapi::modifyRange(
        context$selection[[1]]$range, paste0(out, collapse = "\n"),
        id = context$id
    )
}

#' get_rstudio_context
#'
#' from {https://github.com/r-lib/styler/blob/0154a18943c09b99dd67e90080edaa2fe358af97/R/addins.R}
#'
#' @return getActiveDocumentContext
#'
get_rstudio_context <- function() {
    rstudioapi::getActiveDocumentContext()
}
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

This workaround is now out of date.

For the latest solution, check out

1 Like