5
$\begingroup$

RStudio automatically recognizes headers in an R script that are set via comments:

enter image description here

I would like to exploit that feature, but I don't quite understand what the rules are for RStudio to recognize them as headers. Can someone explain?

$\endgroup$
8
$\begingroup$

Check out Code Folding and Sections:

Code sections allow you to break a larger source file into a set of discrete regions for easy navigation between them. Code sections are automatically foldable—for example, the following source file has three sections (one expanded and the other two folded):

To insert a new code section you can use the Code -> Insert Section command. Alternatively, any comment line which includes at least four trailing dashes (-), equal signs (=), or pound signs (#) automatically creates a code section. For example, all of the following lines create code sections:

# Section One ---------------------------------
# Section Two =================================
### Section Three #############################

Note that as illustrated above the line can start with any number of pound signs (#) so long as it ends with four or more -, =, or # characters.

(highlights by myself)

$\endgroup$
1
  • 3
    $\begingroup$ Excellent answer. One tidbit to add is that the RStudio does not appear to feature hierarchically nested code section. That is, regardless of how many ='s, -'s, or #'s you add (as long as it's at least 4), all code sections will appear sequentially at the same level of organization. $\endgroup$ Mar 3 '17 at 15:30
3
$\begingroup$

RStudio seems to recognize subheaders when they lie within functions. For example:

# SECTION ONE -----------------------------------------------------------------

testfunc <- function(input1,input2,input3){

# SUBSECTION ONE --------------------------------------------------------------
# SUBSECTION TWO --------------------------------------------------------------
  ss2func <- function(x1,x2,x3){

  }
}

# SECTION TWO -----------------------------------------------------------------
$\endgroup$
3
$\begingroup$

It's kind of ugly but if you want a subsection to fold up inside the section but also be foldable on its own you can use curly braces at either end of the subsection.

# SECTION ONE --------------------------------------------------------------
y <- 11:20

{# SUBSECTION ONE ----------------------------------------------------------
x <- 1:10
}   

# SECTION TWO --------------------------------------------------------------

This way you can have subsection one folded while still seeing the code in section one. But if you fold section one subsection one will also be closed.

$\endgroup$
1
$\begingroup$

To have sections, subsections, sub-sub sections

# My Section 1----------
This is my section
#* My Section 1.1-----------
This is my sub section
#** My Section 1.1.1-------------
This is my sub sub section

For further sub-sub-sub....divisions, Just add another star to the beginning of the line..!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.