how to place matching parenthesis vertically below?

I am having a harrowing time debugging with around 7-8 levels of nesting parenthesis in a shiny app code.

I use CMD-i to constantly re index the code.

But I see Rstudio does not place the closing braces directly below the opening quote. I cannot understand the logic behind this design.

To make my point clear I show a small clip of my code, first as RStudio produces it, and then as I would like to see.
RStudio output:

My desired output:

i would like to know others views on this problem.

That's odd... I set up a test case to try and reproduce, and also to test the difference between ctrl + i and ctrl + shift + a which is "reformat code".

I started with a mal-indented nested list:


if (a > b) {
  mylist <- list(thing1=1, 
                 thing5 = list(inner1=1, 
                               inner3=list(innerinner1=1
                               )
                                     )
                                           )
  
  
} 

first I tried ctrl + i and, oddly, I think I got the indentation you were looking for


if (a > b) {
  mylist <- list(thing1=1, 
                 thing5 = list(inner1=1, 
                               inner3=list(innerinner1=1
                               )
                 )
  )
  
  
} 

Then I tried cmd + shift + a and it didn't like my closing parens on different lines:

if (a > b) {
  mylist <- list(thing1 = 1,
                 thing5 = list(inner1 = 1,
                               inner3 = list(innerinner1 = 1)))
  
  
} 

So why isn't yours right? Let's make a strawman that looks more like yours:

if (a > b) {
  options(DT.options = list(lageLength=3, 
                            innerlist = list(a=1,b=2)
                 )
                     )

Now if I do ctrl + i here:


if (a > b) {
  options(DT.options = list(lageLength=3, 
                            innerlist = list(a=1,b=2)
  )
  )

oh... well that replicates.

If I do ctrl + shift + a I get closing parens like previously:



if (a > b) {
  options(DT.options = list(lageLength = 3,
                            innerlist = list(a = 1, b = 2)))

however, I found that if I do a linefeed after the DT.Options = then it behaves properly:


if (a > b) {
  options(DT.options = 
            list(lageLength = 3,
                 innerlist = list(a = 1, b = 2)
            )
  )

I haven't the slightest knowledge of how code formatting logic works on the inside. But it looks like having two levels of nesting on the same line (options(DT.options = list(lageLength = 3,) throws it a curveball and it doesn't know what to do.

2 Likes

Yes, your attempt to reformat after a newline after options = did result in a better indentation..
Will continue trying with as many linefeeds as I can visibly allow and see how it goes. Let's keep this thread alive if someone else wants to comment.

It would be helpful if you could include a reproducible example (preferably, the whole document as text and not a screenshot)

Yeah that's ideal for the initial post, but I reproduced his issue here:

if (a > b) {
  options(DT.options = list(lageLength=3, 
                            innerlist = list(a=1,b=2)
  )
  )

Sorry, I missed that! I see the behavior described with that example.

I think this is the 'intended' behavior, although it could be improved. RStudio always aligns parentheses based on the first non-whitespace character on the line containing the matching parenthesis, and since the two opening parentheses lie on the same line, we see the indentation as in that example.

This is typically what you want, e.g.

list(
    list(
        a = 1,
        b = 2
    )
)

as opposed to

list(
    list(
        a = 1,
        b = 2
        )
    )

although one could argue that perhaps we should break the rules a bit for cases where multiple parentheses lie on the same line, but this does complicate things.

FWIW the more typical closing parenthesis location we see in vertically aligned argument lists is e.g.

list(a = 1,
     b = 2,
     c = 3)

that is, the closing parenthesis does not lie on its own line. Once we put it on its own line, the chosen indentation perhaps is no longer ideal -- we do this:

list(a = 1,
     b = 2,
     c = 3
)

but perhaps this is preferred at least in some cases:

list(a = 1,
     b = 2,
     c = 3
     )

I, too, would like to be able to change this logic of auto-indentation.

It is a nice feature of RStudio, but it is different from the way it works in other IDEs I know. It is different from most style guides on how to format code, too. For example Googles Style Guide for R, which is used at my university. Or the one from tidyverse.
I typically end up manually "fixing" the alignment of all ending parentheses or just turn off the feature altogether, which is a shame.

There is a package called styler from Lorenz Walthert which you can use to change the style of existing code. But it would be nice if there was an option within RStudio itself.

Maybe Iā€™m missing something, but the RStudio IDE default seems to me to be in line with the recommended tidyverse style: 2 Syntax | The tidyverse style guide

But I agree it would be nice if you could specify some sort of profile for these aspects of style (maybe based on styler rules?).

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