5

If a command in R is too long, is it possible to write the rest of it to the next line?

For example

tmp   =  -0.00773 + 1.5657  -0.9391  + 0.4753   - 0.1019   -0.00495

tmp   =  -0.00773 + 1.5657  -0.9391  
         + 0.4753   - 0.1019   -0.00495

will give different results.

Thanks!

  • 1
    didn't it occur to you to try keeping the `+` on the first line? – rawr Mar 12 '14 at 23:14
  • not me. I've seen coding styles in other languages that encourage putting the operator on the beginning of the next line. – Chris Dec 03 '18 at 15:58

1 Answers1

7

well...

tmp   =  (-0.00773 + 1.5657  -0.9391  + 
            0.4753   - 0.1019   -0.00495)
Paulo E. Cardoso
  • 5,778
  • 32
  • 42
  • 2
    For more complicated case, like you want to break in middle of a variable or function's name, is there a special character, like `...` in some language for continuing to the next line? –  Mar 12 '14 at 22:41
  • The plus sign will work anywhere, including when you split the arguments of a function between lines. For example, see [this](http://stackoverflow.com/questions/14680075/simpler-population-pyramid-in-ggplot2). – dmvianna Mar 12 '14 at 22:42
  • 2
    @dmvianna - I think the `+` sign in that example is a specific overriding of the base `+` function by ggplot. – thelatemail Mar 12 '14 at 22:46
  • @T... I'm not sure I'm understand your idea. Break an object name ? – Paulo E. Cardoso Mar 12 '14 at 22:47
  • 3
    @thelatemail That doesn’t make a difference. ggplot’s overridden `+` doesn’t change the way R parses the expression. – Konrad Rudolph Mar 12 '14 at 22:57
  • 3
    @KonradRudolph - True. I was just pointing out that the `+` is not a generic character for continuation of code, which it has an implied use for specifically in ggplot. A more generic example might have been http://stackoverflow.com/a/6330975/496803 – thelatemail Mar 12 '14 at 23:04
  • 6
    It's like you are putting two band-aids on the issue. Putting the `+` at the end of the first line was enough (parentheses or not) to tell the R parser that it had to keep reading on the next line. Using parentheses also addresses the issue: the R parser knows that it has to read until at least the closing parenthesis. With parentheses, the `+` could have gone either way (at the end of the first line or the start or the second) for the same result. – flodel Mar 12 '14 at 23:48