[R] Coding style query (braces)

Deepayan Sarkar deepayan.sarkar at gmail.com
Thu Oct 26 02:03:36 CEST 2006


On 10/25/06, Mike Prager <mike.prager at noaa.gov> wrote:
> Re:  placement of braces and "else" clauses.  At the R prompt, I
> believe their placement must avoid causing a syntactically
> complete statement at the wrong place.  This can results in what
> might be considered rather awkward looking code.
>
> IF it is known that code will be used via sourcing a script
> only, is there any potential problem with placing braces as
> shown below ?
>
> xxx <- function()
> {
>         blah()
>         if (x > 3)
>                 {
>                 ... if statements here ...
>                 }
>         else
>                 {
>                 ... else statements here ...
>                 }
>
> }       # end function
>
>
> This is code that I would like to work not just now, but in
> years to come.
>
> Comments appreciated.

This looks fine (I use a similar style), as long as it's inside a
function, or more precisely, inside braces.  Note that while this is
an error:

> a
[1] 1
> if (a == 1) print("one")
[1] "one"
> else print(a)
Error: syntax error in "else"

this is perfectly OK:

> {
+ a = 1
+ if (a == 1) print("one")
+ else print(a)
+ }
[1] "one"

-Deepayan



More information about the R-help mailing list