Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

waiting for user input in R from terminal

Tags:

r

rstudio

I was able to wait for user input in R when running my script as Rscipt myscript.R from the command line as follows and reading the input from stdin.

cat("Enter word : ")
word <- readLines(file("stdin"),1)
print(word);

However, when I try to do it from the terminal using the below code, it just goes to the next line without taking user input. How do I overcome this?

word <- readline(prompt="Enter a word: ")
print(word);
like image 250
tubby Avatar asked Apr 09 '15 16:04

tubby


1 Answers

The "user" input is the line after readline. Try this:

word <- readline(prompt="Enter a word: ")
Hello world!
print(word)

Update

To wait for input in the console:

word <- readline(prompt="Enter a word: "); print(word)

or

{
  word <- readline(prompt="Enter a word: ")
  print(word)
}
like image 125
bergant Avatar answered Sep 29 '22 11:09

bergant