[R] syntax for extending a line in a script??

Brian Diggs diggsb at ohsu.edu
Thu Jan 13 00:08:21 CET 2011

On 1/12/2011 2:46 PM, Mike Williamson wrote:
> Hello,
>      A hopefully simple question.  I use 'R' through emacs, but I suspect the
> following would occur with any manner of text editor:
>     - my editor has a normally quite handy feature where it will
>     automatically indent to the appropriate level when I start a new line.
>     However, this occasionally creates cases where there is no friendly way to
>     break a long line of code into two lines which still function as one
>     command.  Therefore, I need a nice way to be able to flag 'R' to know that
>     the code is continuing on the next line.  Let me explain via example:
>          numericColumns<- names(listOfDataFrames[[myDF]][,columnsOI])
>                      [sapply(listOfDataFrames[[myDF]][,columnsOI],
> is.numeric) ]

You can put the right hand side of the assignment in parentheses. Then 
even with the same breaks, the first line is not complete, so R will 
continue parsing.  An emacs still indents reasonably. (I added a second 
line break to try and avoid email wrapping affecting things).

numericColumns <- (names(listOfDataFrames[[myDF]][,columnsOI])

>      As you can see in this case, I would *like* for these 2 lines of code to
> be read as 1 line, but since the "names(<blah>)" command is sufficiently a
> command on its own, 'R' see this as a completed line of code.  I could try
> to break it up at different points, but emacs (and other text editors) takes
> a guess as to the most intelligent way to indent, so that if I were to write
> something like:
>          numericColumns<- names(listOfDataFrames[[myDF]][,columnsOI])
> [sapply(
>                        listOfDataFrames[[myDF]][,columnsOI], is.numeric) ]
> it would actually indent something more like this:
>          numericColumns<- names(listOfDataFrames[[myDF]][,columnsOI])
> [sapply(
> listOfDataFrames[[myDF]][,columnsOI], is.numeric) ]
> and as you can see, that doesn't help the issue of preventing the code from
> wrapping around (and therefore doesn't help readability).  Is there some
> simple way to flag that the next line is continuing?  Something like
> python's "\" at the end of a line?  I tried wrapping the whole thing around
> curly braces { } but that didn't work, either.

Putting the right hand side in curly braces might work too.  That would 
turn it into a code block, which should evaluate to whatever the last 
statement in the code block is (which in this case is the only 
statement).  I wouldn't be surprised if there is some case where curly 
braces might lead to a different result; parentheses shouldn't (but I 
may be wrong).

>                                             Thanks!
>                                                     Mike

Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

More information about the R-help mailing list