R: How to indent multiple lines of code in Rstudio?

Tag: r , rstudio Author: yfmc172585 Date: 2013-05-26

I have tried using "reindent lines" after highlighting the lines but that doesn't work (in fact it removes indentation). For example I want a quick way to

take a script like this

#parameters
b=2
c=1
d=4
q=3

and turn it into this

#parameters
    b=2
    c=1
    d=4
    q=3

in Rstudio

Best Answer

Highlighting the code of interest and hitting tab should add another level of indentation. Shift-tab will remove a level of indentation.

comments:

wow, how incredibly simple. I just assumed that would delete all your code and replace it with a single indentation.

Other Answer1

RStudio will let you use Regex in Find and Replace. You can search for \n and replace with \n\t, which will do what (I think) you want.

The automatic indent is designed to format your code so that there is indenting where appropriate (inside a pair of brackets, for example). 'Reindent' is for when there has been automatic indenting applied which is no longer appropriate (eg brackets have been removed).