Batch reindenting

Hi,

Is there a way to apply the rstudio indenting routine (ie, Ctrl + I or Code / Reindent Lines) on an entire folder of R files rather than having to open each file and using the routine manually?

I think the styler package would be able to help in this regard.

Hi,

Thanks for the suggestion. Not exactly what I am looking for as the style_active_file/dir routine also refactors the code (eg, line break inserts).

I switch between multiple IDEs which each have a different behavior in terms of indenting (especially with the "({ })" structure which are widely used in Shiny code)... Re-indent is all I need :smiley:

Gotcha -- unfortunately, RStudio currently doesn't expose an easy way to access its code indenter for use outside of RStudio.

I wonder if the styler package could be extended to handle cases like this -- just re-indent code without changing any other structure?

Yes, it's totally possible with styler. If you want to apply the spacing rules (which includes indention) of the tidyverse style guide, you can simply

library(styler)
style_text(c(
  "a = 3", 
  "function(x) {",
  "3+1", 
  "}"
), transformers = style_guide)
#> a = 3
#> function(x) {
#>   3 + 1
#> }

or

styler::style_dir(scope = "indention")

To style a directory. If you want just the indention rules, you can also remove all non-indention spacing transformers from the style guide after you created it:

library(styler)
style_guide <- tidyverse_style(scope = "indention")
# indention rules are sub-domain of spaces. Just keep all spacing rules which 
# name contains "indent" and discard all other.
indention_rules <- grep("indent", names(style_guide$space), value = TRUE)
style_guide$space <- style_guide$space[indention_rules]
style_text(c(
  "a = 3", 
  "function(x) {",
  "3+1", 
  "}"
), transformers = style_guide)
#> a = 3
#> function(x) {
#>   3+1
#> }

If you want to indent by - say 4 spaces, you can also do that when you create the style guide like this:

style_guide <- tidyverse_style(scope = "indention", indent_by = 4)

Thank you for your input, but even with the recommended scope, styler does not seem as robust as the RStudio internal formatting tool:

library(styler)
style_guide <- tidyverse_style(scope = "indention")
# RStudio formatting
#a <- 3
#b <- 5
#c <- ifelse(a |
#    b,
#  1,
#  2
#)
text <- c(
  "a <- 3", 
  "b <- 5",
  "c <- ifelse(a|",
  "b,", 
  "1,",
  "2",
  ")"
)
style_text(text, transformers = style_guide)

Well, the thing is that the tidyverse style guide does not encourage this kind of indention:

call(arg, 
     arg2
)

So the Rstudio re-indention is strictly speaking not in line with the tidyverse style guide. In addition, note that your case is special because for ifelse, styler allows an argument to be on the same line as the function name even if the consecutive arguments are unnamed. This is only possible with a few function names we hard-coded.

library(styler)
text <- c(
  "a <- 3",
  "b <- 5",
  "c <- ifelse(a|",
  "b,",
  "1,",
  "2",
  ")"
)
style_text(text)
#> a <- 3
#> b <- 5
#> c <- ifelse(a |
#>   b,
#> 1,
#> 2
#> )

text <- c(
  "a <- 3",
  "b <- 5",
  "c <- g(a|",
  "b,",
  "1,",
  "2",
  ")"
)
style_text(text)
#> a <- 3
#> b <- 5
#> c <- g(
#>   a |
#>     b,
#>   1,
#>   2
#> )

Created on 2019-03-15 by the reprex package (v0.2.1)

Anyways I agree that the styler formatting is probably also not very pleasing in this case. I opened an issue in the styler repo.

Hi,

I may be wrong but I think the problem I highlighted has more to do with how styler or Rstudio indent conditional expressions spanning multiple lines than with the desire to align arguments based upon the function opening parenthesis.