new line between '}' and 'else' in function body
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
--
Yihui Xie <xieyihui at gmail.com>
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building,
Renmin University of China, Beijing, 100872, China
On Fri, Apr 3, 2009 at 2:48 PM, Romain Francois
<romain.francois at dbmail.com> wrote:
Hi, That's because the parser knows how to deal with that stuff. However, when you type the same if/else at the command line, it will be parsed line by line, and the evaluator will not wait for the else to evaluate the if. Try to copy and paste your if/else to the command line. Romain Yihui Xie wrote:
Hi list members,
?"else" tells us
? ? In particular, you should not have a newline between '}' and
? ? 'else' to avoid a syntax error in entering a 'if ... else'
? ? construct at the keyboard or via 'source'.
but there's no syntax error when you break the line between "}" and
"else" in a function, e.g.
f = function(x) {
? ?if (x) {
? ? ? ?1
? ?} # a new line here!
? ?else {
? ? ? ?2
? ?}
}
f(TRUE)
[1] 1
f(FALSE)
[1] 2 Seems strange... Regards, Yihui -- Yihui Xie <xieyihui at gmail.com> Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086 Mobile: +86-15810805877 Homepage: http://www.yihui.name School of Statistics, Room 1037, Mingde Main Building, Renmin University of China, Beijing, 100872, China
-- Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr