[R] deparse() and the 'else' statement

Yihui Xie xieyihui at gmail.com
Mon Nov 9 22:20:07 CET 2009


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 <xieyihui at gmail.com>
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA




More information about the R-help mailing list