[R] new line between '}' and 'else' in function body

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Fri Apr 3 11:49:35 CEST 2009


you can always wrap the whole if/else statement into innocent braces or
parentheses, as in

    y = {
       if (x) 1
       else 2 }

    y = (
       if (x) 1
       else 2 )

it doesn't have to be a function, and there is no need for the
assignment either -- you just need to tell the parser that the input
hasn't ended.  that's a matter of taste, but i find

    { if (x) 1
       else 2 }

more readable than something like

    if (x) { 1
    } else 2

vQ




Yihui Xie wrote:
> Thanks, Romain! So I think, for consistency, the following result
>
>   
>> deparse(parse(text = '
>>     
> + f = function(x) {
> +    if (x) {
> +        1
> +    } # a new line here!
> +    else {
> +        2
> +    }
> + }
> + ')
> + )
> [1] "structure(expression(f = function(x) {" "    if (x) {"
> [3] "        1"                              "    }"
> [5] "    else {"                             "        2"
> [7] "    }"                                  "}), srcfile =
> <environment>)"
>
> should be
>
> [1] "structure(expression(f = function(x) {" "    if (x) {"
> [3] "        1"                              "    } else {"
> [5] "        2"                             "    }"
> [7] "}), srcfile = <environment>)"
>
> instead.
>
> Regards,
> Yihui
>




More information about the R-help mailing list