6

Basically I've written quite an amount of functions in one R-Script. To create a better overview and that i don't have to scroll too much, i usually press the arrow on the left side of a function definition, which folds it. This can be quite annoying to do for over 30 functions as they continiously "unfold" when i run them. What i mean with the folding can be seen in this picture

enter image description here

Does someone know a keyboard shortcut for this problem?

3 Answers 3

12

In Rstudio, you can click on Edit -> Folding -> Collapse All

enter image description here

The keyboard shortcut to that is Command + Option + O on mac.

Sign up to request clarification or add additional context in comments.

1 Comment

This would sadly collapse other parts that I have marked with ### or ---, as well. I'm looking for a solution to only collapse the functions.
3

Not a direct solution to the folding problem, but an easier way to navigating massive scripts is with the drop-down list in the bottom of the source editor. It lists all defined functions in the active window.

enter image description here

Comments

2

Unfortunately, it seem not possible.

I think the best is to learn some rules to apply systematically: RStudio folding rules

A section that one can fold using the "Edit -> Folding -> Collapse All" will be any comment line (starting with #) which includes at least four trailing dashes (-), equal signs (=), or pound signs (#)

Instead of collapsing the { }, one could insert the section as expected by R, then use the "Edit -> Folding -> Collapse All".

Note: this will not work nicely with a selected region and "Edit -> Folding -> Collapse".

From the following code:

# function1 ####
function1 <- function(x){
    somecode <- 0
    test <- 3
    mean(c(1,2,3))
}
# function2 ====
function2 <- function(x){
    somecode <- 0
    test <- 3
    mean(c(1,2,3))
}
# function3 ----
function3 <- function(x){
    somecode <- 0
    test <- 3
    mean(c(1,2,3))
}
# function4 ####
function4 <- function(x){
    somecode <- 0
    test <- 3
    mean(c(1,2,3))
}
#### function5 ####
function5 <- function(x){
    somecode <- 0
    test <- 3
    mean(c(1,2,3))
}

I can fold it this way:

Folded code Rstudio

If one always use the same way to define function, it is possible to use awk to actually add a section before each function definition:

awk '{ if( $0 ~/function/ ) { print "####" $0 "\n" $0 }else{ print $0} }' code_function.R

From the following code contained in the file code_function.R

function1 <- function(x){
    somecode <- 0
    test <- 3
    mean(c(1,2,3))
}
function2 <- function(x){
    somecode <- 0
    test <- 3
    mean(c(1,2,3))
}

I run in a (linux) terminal:

awk '{ if( $0 ~/function/ ) { print "####" $0 "\n" $0 }else{ print $0} }' code_function.R > code_function_withSections.R

and I obtain the following file -- ugly file -- but a fast way to modify a 10000-functions file.

####function1 <- function(x){
function1 <- function(x){
    somecode <- 0
    test <- 3
    mean(c(1,2,3))
}
####function2 <- function(x){
function2 <- function(x){
    somecode <- 0
    test <- 3
    mean(c(1,2,3))
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.