Yes, you just have modify the connection to stdin()
, like this:
readline2 = function(prompt=""){
cat(prompt)
readLines(stdin(),1)
}
readline2("enter a number: ")
Pregunta
This seems to work on Linux but not on Windows (at least not when I start it from inside RStudio):
readline2 = function(prompt=""){
cat(prompt)
readLines("stdin", 1)
}
print(readline2("enter a number: "))
# enter a number: 42
# [1] "42"
Even when you type in a number and press return it won't come back and R has to be killed by pushing the "STOP" button :-(
My question: Can I also make this work on Windows? And if yes how?
Solución
Yes, you just have modify the connection to stdin()
, like this:
readline2 = function(prompt=""){
cat(prompt)
readLines(stdin(),1)
}
readline2("enter a number: ")