Auto-format R code in RStudio Auto-format R code in RStudio r r

Auto-format R code in RStudio


update: June-22-2018

Thank you @Lorenz@kirill@yuhi for styler package. I have used it for a while. The simplest after installation of the package is to just use

scroll to Addin --> style active file

Customization options via interface would give some control on styling we prefer.

Rstudio can now format code to look neat. Select the lines of interest and then navigate to Code >> Reformat code or use the keyboard shortcut Ctrl + Shift + A.

update:This is a good way to re-structure the code, but it breaks at , for the elements of a vector. For few this is OK, but with many elements passed to a vector, it is overkill:

x <- c(  "p.G12C",  "p.F121S",  "p.P124S",  "p.P124L",  "p.E13D",  "p.E203K",  "p.Q209P",  "p.Q209P",  "p.Q209L")

Update: R-Studio Version 0.99.893

There is a new feature that has been added by R-studio Addins. Part of this addins, now you can add @yuhi formatR as an Addin. This is more tidy and cleaner way to structure code than the built-in code >> Refromat code. However, the drawback with the Addin Reformat R Code it throws an error for Rshiny codes.


First CTRL+A, then CTRL+SHIFT+A.


Go to the Code menu and select

Reindent Lines

Under my OS, this has the shortcut Ctrl + I.