when reading data from standard input ( stdin ), they are duplicated in standard output ( stdout ). examples:
$ echo "10" | r -e 'x<-readLines()' 10 $ echo "10" | r -e 'x<-scan(quiet=T)' 1: 10 2:
How to avoid this duplication?
Although it is written in the description of both functions that the default reading is from stdin , you should explicitly open stdin using the file()
function and pass the object it created to the mentioned reading functions (argument con
for readLines()
and file
for scan()
).
$ echo "10" | r -e 'x<-readLines(con=file("stdin"))' $ echo "10" | r -e 'x<-scan(file=file("stdin"),quiet=T)' $
Examples supplemented with real processing of the entered data and output of results:
$ echo "10" | r -e 'x<-readLines(con=file("stdin")); cat(as.numeric(x[1])+1)' 11 $ echo "10" | r -e 'x<-scan(file=file("stdin"),quiet=T); cat(x[1]+1)' 11
ps in the case of the scan()
function, you can even do without calling the file()
function, immediately passing the value stdin
file
argument:
$ echo "10" | r -e 'x<-scan(file="stdin",quiet=T); cat(x[1]+1)' 11
Source: https://ru.stackoverflow.com/questions/979995/
All Articles