So, I’ve been using the RStudio IDE lately, and I really like it. In writing R code, I’ve been striving to keep it simple and easy to read. However, some of my scripts are just getting a little visually overwhelming, especially when I switch to working on my home computer (1280×1024, vs. 2 1920×1080 panels).
I noticed that RStudio supports collapsing code blocks when the curly brace is used, and I tested surrounding different blocks of code with braces. RStudio promptly indented them for me and then collapsed them upon request. It didn’t seem to affect the code at all – still ran just the same.
I know that I could take the code blocks in question and create functions out of them, which might accomplish the same thing (collapse the function), but I was curious if there was any reason why I shouldn’t use the curly braces in this way? I know that curly braces have been shown to speed up R computation (as described here), but I’m not sure how it would affect my programs.
A good way to create collapsable code blocks is to use code sections. The hotkey to create a code section is CRTL + SHIFT + R. An alternative way is to split up your longer script into a number of functions. These functions can also be collapsed.