formatR icon indicating copy to clipboard operation
formatR copied to clipboard

how to split the parameters into different lines and keep them aligned?

Open fyears opened this issue 7 years ago • 9 comments

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)
}

fyears avatar Aug 15 '16 01:08 fyears

formatR cannot do that...


Reminder to self: update this post when this feature is implemented.

yihui avatar Aug 15 '16 04:08 yihui

I second adding this feature if possible, since this is the correct way to write functions with multi-line parameters

jfiksel avatar Feb 09 '17 16:02 jfiksel

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...

yihui avatar Feb 09 '17 16:02 yihui

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...

fyears avatar Feb 09 '17 19:02 fyears

Exactly. You cannot do this only through regular expressions or any text processing techniques. A parser has to be involved.

yihui avatar Feb 09 '17 20:02 yihui

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

jfiksel avatar Feb 09 '17 20:02 jfiksel

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.

yihui avatar Feb 10 '17 20:02 yihui

It's this basically a duplicate of #53?

naught101 avatar Mar 22 '17 05:03 naught101

@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.

yihui avatar Mar 22 '17 05:03 yihui