Search code examples
rcode-folding

How to code fold parenthesis () in R / RShiny App using RStudio hot key?


I use code folding + the code folding hot key all the time in R to keep my scripts organized. If everything is in functions and the functions do the work, the folded code is very neat.

How can we code fold the dashboardBody() call below? If I add ##### between lines 32 & 33, the entire script below line 33 folds into one fold which is problematic, as I just want the dashboardBody() function to fold into itself here.

enter image description here

I tried this where I wrapped the whole thing in curly braces

ui_body <- {dashboardBody(

  # Shiny HTML Tags 
  tags$script(HTML("$('body').addClass('fixed');")),
  tags$head(tags$style(HTML(css_fixed_sidebar))),
  tags$head(tags$style(HTML(css_dashboard_body_white))),
  tags$head(tags$style(HTML(css_nyk_grey_header))),
  
  # UI Body for this page
  tabItems(
    create_ui__glossary(id = NA) # NA for no server module
  )
)}

...and while the code-folding icon does appear here

enter image description here

...it is problematic because (a) it would be better if dashboardBody() still showed, like how the function names show when they are collapsed (although this may not be possible), and more importantly (b) the RStudio code-folding hot key (ALT-O on Windows) for some reason doesn't work to fold this section for some reason, and so I have to manually click the arrow to fold this code, which defeats the purpose of using the hot-key to code-fold entire scripts.

Is there another approach?


Solution

  • Wrap it in a function and fold the function...

    create_ui_body <- function() {
      return (
        dashboardBody(
           ...
        )
      )
    }
    ui_body <- create_ui_body()
    

    Folds into this:

    enter image description here

    I'd be interested to hear if anybody can point to any issues with this approach.