deparse() and the 'else' statement

2 messages Options
Embed this post
Permalink
Yihui Xie

deparse() and the 'else' statement

Reply Threaded More More options
Print post
Permalink
Hi all,

It is recommended in ?'if' that we use 'else' right after '}' instead
of starting a new line, but I noticed deparse() will separate '}' and
'else' when the 'if...else' clause is used inside {...} (e.g. function
body). Here is an example:

## if/else inside {}
> cat(deparse(parse(text='function(){if (TRUE) {1} else {2}}')), sep='\n')
structure(expression(function() {
    if (TRUE) {
        1
    }
    else {
        2
    }
}), srcfile = <environment>)
## no {}
> cat(deparse(parse(text='if (TRUE) {1} else {2}')), sep='\n')
structure(expression(if (TRUE) {
    1
} else {
    2
}), srcfile = <environment>)

I wonder if there is a way to let 'else' stay with '}' when deparsing
the expression. Thanks!

> sessionInfo()
R version 2.10.0 (2009-10-26)
i386-pc-mingw32

locale:
[1] LC_COLLATE=Chinese_People's Republic of China.936
[2] LC_CTYPE=Chinese_People's Republic of China.936
[3] LC_MONETARY=Chinese_People's Republic of China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese_People's Republic of China.936

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] animation_1.0-8 MASS_7.3-3

loaded via a namespace (and not attached):
[1] tools_2.10.0

Regards,
Yihui
--
Yihui Xie <[hidden email]>
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Duncan Murdoch

Re: deparse() and the 'else' statement

Reply Threaded More More options
Print post
Permalink
On 09/11/2009 4:20 PM, Yihui Xie wrote:

> Hi all,
>
> It is recommended in ?'if' that we use 'else' right after '}' instead
> of starting a new line, but I noticed deparse() will separate '}' and
> 'else' when the 'if...else' clause is used inside {...} (e.g. function
> body). Here is an example:
>
> ## if/else inside {}
>> cat(deparse(parse(text='function(){if (TRUE) {1} else {2}}')), sep='\n')
> structure(expression(function() {
>     if (TRUE) {
>         1
>     }
>     else {
>         2
>     }
> }), srcfile = <environment>)
> ## no {}
>> cat(deparse(parse(text='if (TRUE) {1} else {2}')), sep='\n')
> structure(expression(if (TRUE) {
>     1
> } else {
>     2
> }), srcfile = <environment>)
>
> I wonder if there is a way to let 'else' stay with '}' when deparsing
> the expression. Thanks!

No, there isn't.  You're welcome to submit a well-tested patch, but
please accompany it with a stronger argument in its favour.  Just
because humans are advised to type their code one way doesn't mean R
should, as long as it parses correctly.

Duncan Murdoch

>
>> sessionInfo()
> R version 2.10.0 (2009-10-26)
> i386-pc-mingw32
>
> locale:
> [1] LC_COLLATE=Chinese_People's Republic of China.936
> [2] LC_CTYPE=Chinese_People's Republic of China.936
> [3] LC_MONETARY=Chinese_People's Republic of China.936
> [4] LC_NUMERIC=C
> [5] LC_TIME=Chinese_People's Republic of China.936
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] animation_1.0-8 MASS_7.3-3
>
> loaded via a namespace (and not attached):
> [1] tools_2.10.0
>
> Regards,
> Yihui
> --
> Yihui Xie <[hidden email]>
> Phone: 515-294-6609 Web: http://yihui.name
> Department of Statistics, Iowa State University
> 3211 Snedecor Hall, Ames, IA
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.