rformattingrstudior-styler

Get R styler package to remember settings


I am trying to get the r package styler to remember a simple alteration to tidyverse_style(), i.e.

styler::tidyverse_style(indent_by = 4L)

When using the rstudio addin tab addins > Set style and inputting the above option, styler remembers to indent by 4 spaces for each tab, but forgets this setting when Rstudio is closed.

If I input the above line in the console, it does not remember the 4L indentation at all, defaulting to 2L.

I have tried caching and that doesn't work, probably because all it is used for is to prevent redundant linting.

> styler::cache_activate()
Using cache 1.9.0 at C:\Users\<user>\AppData\Local\R\cache\R\R.cache/styler/1.9.0.
> styler::cache_info()
Size:       0 bytes (0 cached expressions)
Last modified:  NA
Created:    2023-01-16 09:57:37
Location:   C:\Users\<user>\AppData\Local\R\cache\R\R.cache/styler/1.9.0
Activated:  TRUE

I have trawled through the documentation and cannot find a simple way to store styler settings and call it up everytime Rstudio starts up. It would be silly if the only way to do this is to write a styler package and import it with library(mystyle).


Solution

  • From the documentation of the styler package you have to set styler.addins_style_transformer in your .Rprofile:

    The style transformers are memorized within an R session via the R option styler.addins_style_transformer so if you want it to persist over sessions, set the option styler.addins_style_transformer in your .Rprofile.

    Following the example in the docs add e.g.

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

    to your .Rprofile.