rmacrosrstudiocode-organization

Create Shortcut For Code Folding Curly Brackets


I use the curly brackets {} to help organize my R-code. The collapse/expand all macro does not work on curly brackets. I am aware that I could make addins and assign them to a shortcut / keybinding. Is there a way for me to create a shortcut (i.e. using addins) that will collapse/expand all {} sections of my code?

Example:

Prior to pressing shortcut:

x = 4

# Adds 2
{
    x = x + 2
}

After pressing shorcut:

x = 4

# Adds 2
{ ... }

Solution

  • I found the answer here: https://docs.rstudio.com/ide/server-pro/latest/rstudio_ide_commands/rstudio_ide_commands.html

    Example:

    rstudioapi::executeCommand(commandId = "fold")
    

    This lets you fold the currently selected section of code. Using other basic commands from rstudioapi, I can easily build an addin that does what I want.