Rstudio document outline not indenting when I would expect it to

I would like to use the document outline feature in RStudio to more effectively allow my code reviewer to navigate a file, but my outline is not indenting like I would expect it to. I'm wondering if it's possible to get the output I want by changing my syntax. I posted this question to stackoverflow with pictures, it might be easier to see there. I put the code below with an example of what I would like the outline to look like at the bottom. Thanks for your help!

Paste code below and hit ctrl+shift+O, first few sections are indented under my_function, but then the remaining sections are even with my_function, not sure why since they are also nested inside my_function.

my_function <- function(x,y){
  # Unconditional ====
  print("Set some variables for the function")
  # Condition 1 ====
  if(x>100){
    # Action 1 ====  
    print("Do some stuff to x")
    # Action 2 ====  
    z <- x*pi
    }
  # Condition 2 ====
  else if(y<=100){
    # Action 1 ====  
    print("Do some stuff to x")
    # Action 2 ====  
    z <- psigamma(y)
    }
  # Condition 3 ====
  else if(x<=100){
    # Action 1 ====  
    print("Do some stuff to x")
    # Action 2 ====
    z <- x+100
  }
  # Otherwise ====
  else{
    z <- x+y
  }
  return(z)
}

I would like the outline to look like this:

my_function

  • Unconditional
  • Condition 1
    -- Action 1
    -- Action 2
  • Condition 2
    -- Action 1
    -- Action 2
  • Condition 3
    -- Action 1
    -- Action 2
  • Otherwise

The issue here is that RStudio only considers function bodies (not all braces) in terms of where it decides to indent sections.

Would you mind filing this as a feature request at https://github.com/rstudio/rstudio/issues?

Sure @kevinushey, sorry for the delayed response and thanks for the feedback!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.