An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110112/a3ea9825/attachment.pl>
syntax for extending a line in a script??
4 messages · Brian Diggs, David Winsemius, Mike Williamson
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])
[sapply(listOfDataFrames[[myDF]][,columnsOI],
is.numeric)])
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
On Jan 12, 2011, at 5: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:
My practice is to use the opening of a paired code delimiter like "["
or "(" at the end of a line as I have modified your code to show:
numericColumns <- names(listOfDataFrames[[myDF]][,columnsOI])[
sapply(listOfDataFrames[[myDF]]
[,columnsOI],
is.numeric) ]
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.
Thanks!
Mike
"Telescopes and bathyscaphes and sonar probes of Scottish lakes,
Tacoma Narrows bridge collapse explained with abstract phase-space
maps,
Some x-ray slides, a music score, Minard's Napoleanic war:
The most exciting frontier is charting what's already here."
-- xkcd
--
Help protect Wikipedia. Donate now:
http://wikimediafoundation.org/wiki/Support_Wikipedia/en
[[alternative HTML version deleted]]
______________________________________________ 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.
David Winsemius, MD West Hartford, CT
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110112/7bd786c7/attachment.pl>