I am searching for a way to get user input inside a loop while executing in batch mode.
readLines()
and scan()
work well for me in interactive mode only, in batch mode they start to read in lines of code as user input, unless all the code is surrounded by {}
, which is inconvenient. I need a simple solution to get just 1 integer value in a way that I can just type in value and press ENTER, so
I can't find a way to do it that will satisfy both conditions, e.g. ginput()
from gWidgets activates the input field, but ENTER doesn't trigger form submission.
Here is how I solved my own problem:
require(gWidgets)
options(guiToolkit="RGtk2")
INPUT <- function(message) {
CHOICE <- NA
w <- gbasicdialog(title=message, handler = function(h,...) CHOICE <<- svalue(input))
input <- gedit("", initial.msg="", cont=w, width=10)
addHandlerChanged(input, handler=function (h, ...) {
CHOICE <<- svalue(input)
dispose(w)
})
visible(w, set=TRUE)
return(CHOICE)
}
repeat{
x=INPUT("Input an integer")
if(!is.na(as.integer(x))) break
}
print(x)