[R] A statement over multiple lines (i.e. the ... feature in

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Thu Oct 5 10:18:54 CEST 2006


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-Jin

Just 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 ------------------------------



More information about the R-help mailing list