Wee-Jin Goh
2006-Oct-05 07:28 UTC
[R] A statement over multiple lines (i.e. the ... feature in Matlab)
Hello again list, I thought I'd start a new thread, seeing as it's completely different from my previous question. Some functions I have written require many parameters, and so do not fit nicely into an 80 column width display. This is usually avoided, by spreading that particular statement over a few lines. This is something that I do in Matlab with the following: myFunc( parameter1, ... parameter2, ... parameter3, ... parameter4) The ... operator in Matlab allows me to spread a statement over several lines. The ... operator in R seems to be more like the ... operator in C, which allows for a variable argument list. How do I accomplish this task in R? It's not a show stopper, but it would make reading my code much much easier. Cheers, Wee-Jin
Robin Hankin
2006-Oct-05 07:39 UTC
[R] A statement over multiple lines (i.e. the ... feature in Matlab)
Hi Wee-Jin you can block out bits of R code with if(FALSE){ <code not executed> } For the line breaking, R deals with incomplete lines by not executing the statement until you finish it. In the function case, it waits for you to close a bracket. If you type: myFunc(a=3, b=5, c=6, d=7 ) myFunc() will only execute when you close the bracket HTH rksh On 5 Oct 2006, at 08:28, Wee-Jin Goh wrote:> Hello again list, > > I thought I'd start a new thread, seeing as it's completely different > from my previous question. Some functions I have written require many > parameters, and so do not fit nicely into an 80 column width display. > This is usually avoided, by spreading that particular statement over > a few lines. This is something that I do in Matlab with the following: > > myFunc( parameter1, ... > parameter2, ... > parameter3, ... > parameter4) > > The ... operator in Matlab allows me to spread a statement over > several lines. The ... operator in R seems to be more like the ... > operator in C, which allows for a variable argument list. > > How do I accomplish this task in R? It's not a show stopper, but it > would make reading my code much much easier. > > Cheers, > Wee-Jin > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
(Ted Harding)
2006-Oct-05 08:18 UTC
[R] A statement over multiple lines (i.e. the ... feature in
On 05-Oct-06 Wee-Jin Goh wrote:> Hello again list, > > I thought I'd start a new thread, seeing as it's completely different > from my previous question. Some functions I have written require many > parameters, and so do not fit nicely into an 80 column width display. > This is usually avoided, by spreading that particular statement over > a few lines. This is something that I do in Matlab with the following: > > myFunc( parameter1, ... > parameter2, ... > parameter3, ... > parameter4) > > The ... operator in Matlab allows me to spread a statement over > several lines. The ... operator in R seems to be more like the ... > operator in C, which allows for a variable argument list. > > How do I accomplish this task in R? It's not a show stopper, but it > would make reading my code much much easier. > > Cheers, > Wee-JinJust spread it over several lines, without the "...", which in R you do not need for this purpose: myFunc(parameter1, parameter2, parameter3, parameter4) The point is that R uses its syntactic rules to determine when a component of an expression is complete. Therefore (in this instance) the opening "(" must at some stage be matched by a ")", so, whether or not you interpolate newlines, so R will continue to expect you to input more items until ")" is encountered. Since you can (at any stage of input) have incomplete expressions within incomplete expressions, the same principle applies until every thing is closed up, to the outermost level. Since R *does* use "..." for a "variable argument list", you can of course use this as well, for that purpose. The important thing to remember, though, is that an expression which is incomplete from the point of view of your intentions may look complete to R. So, if you want to break it in the middle, make sure you do so at a point where it is incomplete from R's point of view. For example, suppose you intend x <- 3*4*5*6*7*8 If, in R, you put x <- 2*3*4* 5*6*7*8 in your input file, it will work: > x <- 2*3*4* + 5*6*7*8 > x [1] 40320 > because, syntactically, the "*" at the end of the first line is expected to be followed by something, so R waits for that. But if you put it in as x <- 2*3*4 *5*6*7*8 it will not, because the first line is already complete when the newline is encountered, and then the second line will generate a syntax error because the initial "*" is required to be preceded by something. Having come to the end of a complete valid expression, R is now waiting for the start of a new valid expression; and an expression which starts with a "*" is not valid. Hence: > x <- 2*3*4 > *5*6*7*8 Error: syntax error > x [1] 24 Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 05-Oct-06 Time: 09:18:51 ------------------------------ XFMail ------------------------------