I normally use matlab GUI for programming and it has very nice code folding features, such as the following:
%% This is one chunk of code I can fold just because I am using the %% comment format.
matrix = [1 2 3; 4 5 6];
vector = [1 2];
ax = vector*matrix;
%% This is another chunk of code I can fold because I am using the %% comment format.
matrix2 = [7 8 9; 10 11 12];
vector2 = [7 8];
ax2 = vector2*matrix2;
As mentioned in the matlab code, the %% comment will immediatly give the matlab GUI the folding option and a plus/minus sign for unfolding/folding will appear on that line with the comment visible.
Is it anyway to achieve such formatting in rstudio or a good R gui?
I’d like to have the plus/minus feature, and the fact that the first line comment is visible so I can know what’s in the folded chunk of code.
Thanks.
Photos of the GUI minus/plus unfolded/folded attached bellow:


When I try the same approach in RStudio, seems that the only option is edit -> folding collapse/expand but it hides the comments, and its not as automatable as the %% feature. In my case is more of a problem because I use X11 so I do not want to have to go to edit -> folding… all the time, but rather automate as in matlab using %%.
Have you seen the code folding section of the RStudio documentation? Everything you ask for is described therein.
To directly quote the most relevant section of the documentation, you can create a code section on any comment line by
including at least four trailing dashes (-), equal signs (=), or pound signs (#) automatically creates a code section.If that wasn’t clear enough, maybe we can learn by example:
To illustrate that this actually works further, please examine the following two screenshots. If you are getting something different, I suggest you update RStudio and then follow up directly with the good folks at RStudio:
Expanded:

Collapsed: