Skip to content
Prev 366194 / 398502 Next

Getting scan() -command to work during the program run

As "part of a program" there is no console input file when run in non-interactive mode. Efforts to make a non-interactive session into an interactive session are well documented in the rhelp archives. Search:

http://markmail.org/search/?q=list%3Aorg.r-project.r-help+user+input+interactive+rscript

If you did that inside a function which you ran inside an interactive session and did not return the value there would of course be no value, but that would not give you response you report. You would be expected to use the 'file'-parameter for specifying the source of input when using scan in a script.

If my only code is a single line in a text file named 'Untitled.R':

 X1 <- scan(n=1)

Running just that with Rscript at the system console does give me:

Read 0 items

Now make a data file named 'myfile.txt' and a source file named "Untitled.R" and use it to create a data file named myfile.txt and then read from it.

------
 cat("5", "\n", file="myfile.txt")
 X1 <- scan(file="myfile.txt")
 X1
-------

The source Untitled.R or run with Rscript.