I am trying to write an interactive R script. For example:
try.R:
print(\"Entr some numbers. >\",quote=F)
a = scan(what=double(0))
print a
q()
Try this:
cat("What's your name? ")
x <- readLines(file("stdin"),1)
print(x)
Hopefully some variant of that works for you.
What worked for me on Windows with RStudio 0.98.945 and R version 3.1.1 was:
cat("What's your name? ")
x <- readLines(con=stdin(),1)
print(x)
The answer by @Joshua Ulrich is fine for Linux, but hangs under macOS and needs to be terminated using Ctrl-D.
This is a work-around for both Linux and macOS:
#!/usr/bin/env Rscript
print(system("read -p 'Prompt: ' input; echo $input", intern = TRUE))