2

I am new to R, and despite searching the forums I have been unable to find a solution to indenting code within both the Source window and Document Outline (Ctrl+Shift+O).

An example is shown below.

img1

Ideally, I would want the code to function as below when pressing Alt+O

img2

This function does seem to be implemented in some fashion as you get the indented code with functions but this is less than ideal.

    # Section 1 ----------------------------------------------------------- 
    function(x) {
    # Section 1A  ===========================================================
    }

Has anyone found a work-around to implement this?

Patrick
  • 915
  • 2
  • 9
  • 26
  • interesting. I didn't even know that the document outline existed :D I don't see a way that this is possible - I think the outline is also more specifically for functions, if you have several levels, and to keep an overview about those. A workaround is to add two characters, e.g. underscores before your sub-levels, i.e. `# __Section 1A` – tjebo Sep 04 '18 at 22:47

4 Answers4

2

For what it's worth, this sort of nested indent is implemented for Markdown sections (for e.g. R Markdown documents), e.g.

outline

However, this sort of nested is not implemented for sections in plain R scripts. You might consider filing this as a feature request for the RStudio team.

Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88
  • This was a great suggestion. I posted it as a feature request. If anyone falls onto this question and would like to see this added, you can go +1 the feature [here](https://github.com/rstudio/rstudio/issues/4124) – Patrick Jan 08 '19 at 21:15
2

Not a fix but a workaround:

Any whitespace after a "." is included in the heading, so a "." followed by a tab or space can be used to create indented headers preceded by a ".".

# Section title ---------------------------------------------------------------

# . Subsection A --------------------------------------------------------------

# . . A.1 ---------------------------------------------------------------------

Would still be nice to see this implemented the way it is in R markdown, but in the mean time might make it easier to navigate scripts using sub-headers.

Screenshot of example script using dot-tab to indent headers

Foztarz
  • 71
  • 6
0

Thanks @Foztarz I posted this as an issue about a year ago on the GitHub. They claimed it was a worthy enhancement but they keep pushing it to the next version of RStudio lol. My work-around was similar. I used Alt codes to insert symbols I found a bit more visually appealing over .

 # ▬ Section A ------


    # ▐ ▬ Section A.1-----------

Here's what it looks like inside RStudio

enter image description here

Patrick
  • 915
  • 2
  • 9
  • 26
0

Two additions:

  1. Box-drawing characters look much better than the characters mentioned in https://stackoverflow.com/a/63812437/13776976 @Patricks answer
  2. From RStudio 1.4 on, you can indicate sub-sections by additional '#' at the start of the line, see RStudio How To Article
clemenskuehn
  • 116
  • 8