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

Duncan Murdoch murdoch at stats.uwo.ca
Tue Nov 10 00:48:23 CET 2009


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




More information about the R-help mailing list