Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Studio: organize code in section that can be hidden

Tags:

r

rstudio

I still haven't found a proper way to organize my R code in R studio in sections. I checked the shortcuts that do exist such as ALT + L but it requires the area to be commented.

Ideally, I would like to name line 500 to 700 with a name, hide it and then have it appear back when needed. What would you recommend?

like image 550
Fredkho Avatar asked May 02 '16 22:05

Fredkho


People also ask

How do you make a collapsible section in R?

You can also fold an arbitrary selection of code by using Edit -> Folding -> Collapse (Alt-L). Folded regions are preserved while editing a document; however all foldable regions are shown expanded by default when a file is closed and re-opened.

How do I block code in R?

Example 1: Commenting Out Using Keyboard Shortcut To do this quickly, we first have to highlight the code we want to get rid of and then we have to use the keyboard shortcut Control + Shift + c.

How do I run a section of code in RStudio?

To execute the line of source code where the cursor currently resides you press the Ctrl+Enter key (or use the Run toolbar button): After executing the line of code, RStudio automatically advances the cursor to the next line. This enables you to single-step through a sequence of lines.


1 Answers

This should be pretty easy to achieve. There are likely multiple options for doing this, but here is one way to achieve what you want:

Add a line before you code on line 500 like this:

#### Section Title ####

Then at line 701 (probably line 702 now that you added the comment on line 500) add:

#### end ####

The important parts of this are to start with a # and end with 4 or more #s. I just use 4 on both sides because it looks nicer. Whatever you type in the middle will be your section title.

RStudio will automatically add a little triangle (caret) next to the line number when you type the 4th # after the section title, but clicking this will likely fold everything to the end of your document. If you have another section title later in your document, then it will only fold up to that point. If you start a new section at line 701, then add an appropriate section title, otherwise you can use the "end" section title as I suggest above to achieve the same results.

Note also that once you have made a section title like this you will be able to jump to the start of that section by clicking the dropdown at the bottom left hand of your text editor in RStudio and selecting the desired section from the list.


Alternatively, if you only want to fold code temporarily (and not necessarily have a permanent section that can be folded and unfolded by clicking the caret), then you can just select from line 500 to line 700 and go to the menu bar and select:
Edit > Folding > Collapse
Or use the appropriate shortcut for your operating system (shown in that menu).
This works for both commented text and code.


In answer to the comment regarding the use of CTRL+ALT+F
(my answer was too long for a comment)

As I understand it, CTRL+ALT+F is intended for when you have a code file with non-function code that also contains a function. It will just send only the function where your cursor is to the console. The benefit is that it knows the start and end of the functions because RStudio automatically adds a collapsable section (like the sections created with #### ####) wherever a function starts and ends. But if you add further collapsable sections within this, it may only send the section where your cursor is currently to the console. If you just have a single file with a function in it, you might just check the box that says "Source on Save" and then whenever you hit CTR+S, it will source the function. Alternatively, it will still work with CTRL+ALT+F as long as your cursor is outside of your #### #### sections (such as at the first line of the function). Hope that helps. :)

like image 52
enpitsu Avatar answered Oct 04 '22 19:10

enpitsu