Folding all functions in a R-Studio Script
Asked Answered
S

3

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?

Schrick answered 17/12, 2019 at 8:34 Comment(0)
B
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

Blane answered 17/12, 2019 at 8:39 Comment(0)
D
11

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.

Digitalin answered 17/12, 2019 at 8:42 Comment(1)
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.Schrick
B
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

Blane answered 17/12, 2019 at 8:39 Comment(0)
M
1

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))
}
Mathewson answered 20/5, 2021 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.