I'm wondering whether there is a character to let R know to expect more input for a command on subsequent lines. Here is an example: test_1.R: x <- c(1,2,3,4) / c(1,2,3,4) x R CMD BATCh test_1.R produces test_1.Rout:> x <- c(1,2,3,4) > / c(1,2,3,4)Error: unexpected '/' in " /" Execution halted test_2.R: x <- c(1,2,3,4) / c(1,2,3,4) x R CMD BATCh test_1.R produces test_2.Rout:> x <- c(1,2,3,4) /+ c(1,2,3,4)> x[1] 1 1 1 1 test_2.R works as expected but test_1.R fails because the "/" operator is at the start of line 2 not the end of line 1. Is the some kind of continuation charater one can include in a script so R is expecting input on the next line? For example, in other languages, "\" works. Without this, one is forced to break lines carefully in the middle of a function, for example, to ensure R is expecting more. Any comments very welcome. Thank you, Neil.
On 25/10/2009 8:53 AM, Neil Stewart wrote:> I'm wondering whether there is a character to let R know to expect more > input for a command on subsequent lines. Here is an example:No, the rule R uses is to stop when the statement is complete.> > test_1.R: > x <- c(1,2,3,4) > / c(1,2,3,4) > xThe first line is a complete statement, so R evaluates at that point.> > R CMD BATCh test_1.R produces test_1.Rout: >> x <- c(1,2,3,4) >> / c(1,2,3,4) > Error: unexpected '/' in " /" > Execution halted > > > test_2.R: > x <- c(1,2,3,4) / > c(1,2,3,4) > x > > R CMD BATCh test_1.R produces test_2.Rout: >> x <- c(1,2,3,4) / > + c(1,2,3,4) >> x > [1] 1 1 1 1 > > test_2.R works as expected but test_1.R fails because the "/" operator is at > the start of line 2 not the end of line 1.It would be worse if you had used + or -, because then there would be no syntax error, just an incorrect value in x.> Is the some kind of continuation charater one can include in a script so R > is expecting input on the next line? For example, in other languages, "\" > works. Without this, one is forced to break lines carefully in the middle of > a function, for example, to ensure R is expecting more. Any comments very > welcome.Once you get used to it, it's not hard to write in your test_2.R style. It's probably possible to write an automatic detector for errors like x <- c(1,2,3,4) + c(1,2,3,4) based on heuristics: "no assignment, extra indentation" => warning, but I don't know of an existing one. Duncan Murdoch
On Oct 25, 2009, at 8:53 AM, Neil Stewart wrote:> I'm wondering whether there is a character to let R know to expect > more > input for a command on subsequent lines. Here is an example: > > test_1.R: > x <- c(1,2,3,4) > / c(1,2,3,4) > x > > R CMD BATCh test_1.R produces test_1.Rout: >> x <- c(1,2,3,4) >> / c(1,2,3,4) > Error: unexpected '/' in " /" > Execution halted > > > test_2.R: > x <- c(1,2,3,4) / > c(1,2,3,4) > x > > R CMD BATCh test_1.R produces test_2.Rout: >> x <- c(1,2,3,4) / > + c(1,2,3,4) >> x > [1] 1 1 1 1 > > test_2.R works as expected but test_1.R fails because the "/" > operator is at > the start of line 2 not the end of line 1. > > Is the some kind of continuation charater one can include in a > script so R > is expecting input on the next line? For example, in other > languages, "\" > works. Without this, one is forced to break lines carefully in the > middle of > a function, for example, to ensure R is expecting more. Any comments > very > welcome.R waits for a complete command. Leave the "/" at the end of the first line and it will then be incomplete. It is also common to see ","'s (commas) at the end of lines in scripts. test_1.R: x <- c(1,2,3,4)/ c(1,2,3,4) x # Should "work"> Thank you, > Neil. > > ______________________________________________ > 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 Heritage Laboratories West Hartford, CT
Here are two ways: 1. wrap the line in braces: x <- {.... /...} 2. maybe more awkward in general, but sometimes useful: x <- `/`(... , ...) -Peter Ehlers Neil Stewart wrote:> I'm wondering whether there is a character to let R know to expect more > input for a command on subsequent lines. Here is an example: > > test_1.R: > x <- c(1,2,3,4) > / c(1,2,3,4) > x > > R CMD BATCh test_1.R produces test_1.Rout: >> x <- c(1,2,3,4) >> / c(1,2,3,4) > Error: unexpected '/' in " /" > Execution halted > > > test_2.R: > x <- c(1,2,3,4) / > c(1,2,3,4) > x > > R CMD BATCh test_1.R produces test_2.Rout: >> x <- c(1,2,3,4) / > + c(1,2,3,4) >> x > [1] 1 1 1 1 > > test_2.R works as expected but test_1.R fails because the "/" operator is at > the start of line 2 not the end of line 1. > > Is the some kind of continuation charater one can include in a script so R > is expecting input on the next line? For example, in other languages, "\" > works. Without this, one is forced to break lines carefully in the middle of > a function, for example, to ensure R is expecting more. Any comments very > welcome. > > Thank you, > Neil. > > ______________________________________________ > 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. > >
Neil Stewart wrote: > > I'm wondering whether there is a character to let R know to expect > more input for a command on subsequent lines. Hi Neil, Not that I know of, but I use exactly the method you noticed, the trailing operator. There was a discussion similar to this not long ago about the use of the semicolon as a line terminator. If the trailing operator is a problem, you can wrap the whole statement in parentheses: x<-(c(1,2,3,4) /c(1,2,3,4)) or even: (x<-c(1,2,3,4) /c(1,2,3,4)) The interpreter will assume that more input is needed from the unbalanced parenthesis in the first line. An unbalanced quote would also cause the interpreter to wait for more input, but I don't think it would be of any use in solving your problem. Jim