formatR
formatR copied to clipboard
how to split the parameters into different lines and keep them aligned?
After using "format code" in the latest RStudio, the parameters of functions are split into different lines and kept aligned.
But I am unable to find the corresponding settings in formatR
package. How to do it with formatR
?
Consider the source code:
my_sum = function(a = 1, b = 2, c =3) {
return(a + b + c)
}
The desired formatted result (RStudio returns this):
my_sum = function(a = 1,
b = 2,
c = 3) {
return(a + b + c)
}
I second adding this feature if possible, since this is the correct way to write functions with multi-line parameters
I wouldn't say "it is the correct way", but it is definitely a favorable style to many people.
I don't feel the implementation is easy, so I'll leave this task to contributors...
I checked out the potential solutions before. It was definitely harder than I think: a basic parser understanding R syntax is needed, rather than any "text processing" logics...
Exactly. You cannot do this only through regular expressions or any text processing techniques. A parser has to be involved.
Hm, that sounds way above what I would be capable of doing. It seems that RStudio already implements this, but I am only asking because I have been trying to write R code in Atom for a project that I have to use a remote cluster for. It seems that the atom-beautify package implements formatR for formatting R code
If someone can figure out the RStudio magic, and it turns out not be super difficult to implement in pure R, I'd welcome a pull request. Personally I don't have time or enough interest in this feature.
It's this basically a duplicate of #53?
@naught101 Yes.
I can tell you that RStudio actually uses JavaScript to reformat the R code, so the magic cannot be directly ported to formatR.