Comment out sections in R

I am trying to make my life easier by adding sections in my code by adding comments but I have some issue while doing that. What I am trying to do it for eg I have a section called output tables which has different sections in it . For eg

----Tables
---------- Channel 1
---- Table 1
---- Table 2
---- Table 3
---------- Channel 2
---- Table 1
---- Table 2
---- Table 3
---------- Channel 3
---- Table 1
---- Table 2
---- Table 3

So when I wrap Table sections it should hide all the sections below it. How can I do that? I did follow this blog https://support.rstudio.com/hc/en-us/articles/200484568-Code-Folding-and-Sections but this folds just one section and not multiple sections underneath them.

Hi @sten,

You can't have nested folding in an R script (You can in RMarkdown, so consider that as an alternative). So if you wan to use an R script, you should allow the top-level label to be a section, and everything in that section to simply be a comment. For example:

# Tables ----
# Table 1
...R code...
# Table 2
...R code...

# Figures ----
# Figure 1
... R code...

This will allow you to fold Tables and Figures, and all the code within those sections will be contained in that fold.

In R Markdown, you use H1, H2, H3, etc tags for nested folding:

---
output: html_document
---

# Tables
## Channel 1
### Table 1
```{r}
...R code...
```

### Table 2
### Table 3

## Channel 2
### Table 1
### Table 2
### Table 3

## Channel 3
### Table 1
### Table 2
### Table 3
1 Like

Thank you @mattwarkentin. That works in a way.

I just experimented, and it turns out, plain R does support this, but its achieved by abusing curly braces, you might say.
Try this:

#####
print(1)
##### 
print(2)

{##### curly brace header
  #####
  print(3)
  #####
  print(4)
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.