r-lib / styler

Non-invasive pretty printing of R code
https://styler.r-lib.org
Other
703 stars 70 forks source link

Change default indentation from 2 to 4 #1218

Closed nickhir closed 4 weeks ago

nickhir commented 4 weeks ago

Hello,

I am trying to change the default indentation from 2 spaces to 4 spaces, however I am unable to find an option for this. The solution outlined here does not seem to work anymore.

Any help is much appreciated!

IndrajeetPatil commented 4 weeks ago
library(styler)

style_text(
  "{ x <- 1}",
  transformers = tidyverse_style(indent_by = 2L)
)
#> {
#>   x <- 1
#> }

style_text(
  "{ x <- 1}",
  transformers = tidyverse_style(indent_by = 4L)
)
#> {
#>     x <- 1
#> }

Created on 2024-06-25 with reprex v2.1.0

nickhir commented 4 weeks ago

Thank you for your quick answer! Could you also elaborate, how I can make sure that the add in (Style active file) uses the "new" style (i.e. the one with 4 whitespaces)?

IndrajeetPatil commented 4 weeks ago

You need to use the following option to change addin behaviour:

options(addins_style_transformer = "styler::tidyverse_style(indent_by = 4L)")

If this is in the context of a project, you could also put this in .Rprofile.

nickhir commented 4 weeks ago

Unfortunatly, that did not seem to work... I have a small gif showing it. syler

IndrajeetPatil commented 4 weeks ago

Ah, I forgot to include styler.:

options(styler.addins_style_transformer = "styler::tidyverse_style(indent_by = 4L)")

All of this is documented here: https://styler.r-lib.org/dev/reference/styler_addins.html