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);
The "user" input is the line after readline
.
Try this:
word <- readline(prompt="Enter a word: ")
Hello world!
print(word)
To wait for input in the console:
word <- readline(prompt="Enter a word: "); print(word)
or
{
word <- readline(prompt="Enter a word: ")
print(word)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With