fold code in RStudio

My favorite RStudio tips and tricks

Here is a list of my most used time-saving tips and tricks or just great looking features in RStudio.

Comment or edit multiple lines of R code with RStudio simultaneously

Hold the Alt key and use your cursor to select across the lines of code. Then you can add something, overwrite or delete.

Use Ctrl + 2, Ctrl + 1 to switch from RStudio source pane to console pane and back

Especially handy when you have to do some quick coding in the console and switch back to script.

Fold part of the R code and use section naming

To fold part of the R code, make comment in the desired place and add at least four trailing dash symbols, equal signs (=), or pound signs (#) at the end. Another way to insert folding code sections is by using shortcut Ctrl+Shift+R.

As you see on the right side, there is also a list where you can see all named code sections.

Here is a post about code folding and shortcuts that comes with that.

You can also use the ts code snippet. As I will explain later, you can modify or create new RStudio snippets. Take a look at this post on how exactly do that.

Use Ctrl + Shift + O to access the outlined sections

This is a great tool that allows you to organize or navigate in a script.

Use Tab for code completion

Alt + – to add assignment operator and Ctrl + Shift + M to add pipe operator


Use Up key or Ctrl + Up key to get code history

A great addition to History pane.


Ctrl + left click to view the R object

Of course, you can use View function or click on an object in the environment, but this kind of technique comes handy when Environment pane is busy, and you would like to look at the code to remember the name of the object.

Alt and up or down arrows to move RStudio code lines

Use gc() function to free up RStudio memory

In this other post, I created a custom function to execute gc, clear console, environment, and plot.

Modify RProfile to add missing functionality

If you need custom functions every time RStudio starts, you can modify your RProfile file, as I explained in this post. Customize your RStudio startup.

Use RStudio jobs to run code in parallel

It is super easy to use, and you can do some other things in parallel. Just remember that jobs can return values to a global environment that can lead to conflict with something that you are doing.

Use the filter to clear only some of the environment variables

Very handy when you need to clear some script created objects that come with exact naming principles.


Use RStudio themes to change how it looks

There is an option to install a custom theme to change how RStudio looks. Here is the one that I’m using right now.
You can also go to Tool -> Global Options and choose some of the built-in themes.

Use Ctrl + Shift + A to reflow your code or specially made Addins

By using shortcuts and Addin to reflow your code, you might give different results. I prefer to use them both. If Ctrl + Shift + A not giving the desired result (for example, with pipe operator), then I switch to Addin.

Here is Ctrl + Shift + A.

Here is Styler Addin.

Get some quick help with functions

Move your cursor before, after on into the function and press F1 to get a description on Help pane.

Hover your mouse on a function to see a tooltip with arguments. This especially handy when you are typing something, don’t want to read all the help and need to take a little look at arguments and argument default values.

Use code snippets

For example, if you are writing the if function, you can choose snippets to make it easier.

You can customize them or make a new one by going to Tools -> Global Options -> Code -> Edit Snippets.

Use sound to hear when R script is executed

Sometimes I prefer to add sound when R script execution is finished. For example, function beep from beepr package or PowerShell script.

Use Alt + Shift + K to see a list of other useful RStudio shortcuts as a great addition to these tips and tricks


I hope you liked my favorite RStudio tips and tricks.





Leave a Reply

Your email address will not be published. Required fields are marked *