How to configure RStudio to allow for WhiteSmith indentation?

For details about this indentation pattern called "Whitesmith", please see: or

Using R, here is an example basic function using this indentation style:

# this is "not" recursive at the moment ...
createDir = function (folder)
  ifelse(!dir.exists(folder), dir.create(folder), "Folder exists already")

Here is a switch statement inside a function using the same style:

buildX = function(n,method)
         "rnorm-0-1" = rnorm(n,0,1),
         "rgama-1-0.5" = rgamma(n,1,1/2),
         rnorm(n,0,1) # default case of switch

Here is another function, to conclude the examples:

buildY = function(resp, n, unif, myQ)
  Y = numeric(n);
  for (i in 1:n)
    Y[i] = mapY(i, unif, myQ, resp);

How do I configure RStudio to use this indentation style? Will it work when I copy an paste?

And bonus for an answer to the ability to highlight the two braces more readily, like Notepad++ does? I can click on the right/left side of the open/close brace and it highlights, and even creates a vertical line connecting the braces ...

brace highlighting

I don't believe you can set a particular style convention for R Studio to use, but you might look at the styler package.

Particularly the vignette on customizing styler,

If nothing else, this would allow you to enforce the style after cutting/pasting code or opening a new file.

1 Like

Thanks for the response.

As something with significant programming experience across many languages and IDE's, I find it a bit annoying when I am coding and it "auto-places" the parentheses where it sees fit.

Obviously, it is following some sort of style-convention in its "auto-indent" features. As a programmer, the next step would be to make that an abstract options.

So where are the details regarding how the current "auto-indent" currently works. Is it abstracted as an XML or YAML file. I would be happy to commit to time coding another solution, if I can get pointed in the correct direction.

R Studio is open source, so you're welcome to have at it,

Looking here might be a good place to start,

1 Like

Thanks for the response, I would need to see the bindings and the engine.

Such as ... onclick ... check to see if the immediate character to right/left is a { or } and if so, find its matching pair to highlight

Or ... onenter ... similar checks, and move the { or } based on some indentation schema.

Building an indentation engine is old hat, and using the emacs approach is likely a good approach to develop in the IDE:

(setq c-default-style "whitesmith"
c-basic-offset 4)

You can also disable much of this altogether in the Global Options settings,

This is useful. So if I turn all of those off and type:

function = monte(a,b,c)

And then hit ENTER, it takes me to a new line. I hit TAB and then { ... with those options set, it still moves the { to the left ... either onkeypress or keyup

If I build my closing } the same way and hit ENTER, it moves it back to the left...

So this is why I was asking questions about bindings... are the bindings linked to the Global Code Editing settings?

Making those Global changes has updated how drag n' drop and Copy/Paste work. They work the same now. Before Copy/Past would run auto-indent, but drag n'drop would not.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.