Is it possible to create a foldable hierarchical structure in the document outline of a script?

I am working on a rather big shiny app.

I have created some hierarchy in my section headers (see next picture below) to keep track of it all, however, even when folding the sections i still have to scroll a lot and it's still a little cumbersome to get around.

# headertext  =================================
# headertext  ---------------------------------

So I want to use the document outline, to quickly navigate the doc (scroll up and down essentially).

However, the document outline is not nicely formatted and not so straight forward either.

Thus: Is it possible to create a foldable hierarchical structure in the document outline of a script?

yep - use multiple # at the start of the line

# 1 =====

## 1.1 ====

### 1.1.1 ====

but if it is a shinyapp, it is probably worth reconsidering structure as and when you have the time to refactor - this is a nice explanation of some of the principles that make it easier to manage these things in the long run

Yep, I figured.

Restructuring as we speak.

Your solution does not work for me:

Suppose I want to format this:

# 1 =====
tibble()
## 1.1 ====
tibble()
### 1.1.1 ====
tibble()
# 2 =====
tibble()
## 2.1 ====
tibble()
### 2.1.1 ====
tibble()

When collapsing all folds I get:

# 1 =====
## 1.1 ====
### 1.1.1 ====
# 2 =====
## 2.1 ====
### 2.1.1 ====

I would like to have

# 1 =====
# 2 =====

only since the other ones are supposed to be section-sub-headers...

That is what I mean by foldable hierarchical structure.

Markdown headers (e.g. in Obsidian) collapse every lower-level header beneath them

# Header 1
## Header 1.1
# Header 2
## Header 2.1

collapses to

# Header 1
# Header 2
1 Like

use snippet to define h1, h2, h3------
that will be easy to input

hmm, I see.
from this, you can see what defines "foldability"

Foldable Regions
The following types of code regions are automatically foldable within RStudio:

  • Braced regions (function definitions, conditional blocks, etc.)
  • Code chunks within R Sweave or R Markdown documents
  • Text sections between headers within R Markdown documents
  • Code sections

so if you really want that, I guess you could slap curly braces around whatever you want to include in your hierarchy, but
a) that feels like abusing the system somehow
b) I'm not sure whether it would affect your code at all (I can't see why it would, though)

Personally, rather than folding and scrolling, I find it much easier to navigate either with the document outline (ctrl/cmd + shift + O) or the section navigation menu at the bottom of the script panel, but you know how you like to work.
Screen Shot 2021-08-17 at 8.24.34

Hi,

This is not a direct answer to your question but if the code of your Shiny app is becoming too long, I recommend using Shiny modules. You can learn about them here and here.

1 Like

Thank you. Quickly after, I did exactly that.

1 Like

Thank you.

(I agree, slapping braces around code does not sit right with me either.)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.