User input when executing R-code in batch mode

I am looking for a way to get user input inside a loop at runtime in batch mode.

readLines() and scan() work well for me only in interactive mode, in batch mode they start reading lines of code as user input, if all the code is not surrounded by {} , which is inconvenient. I need a simple solution to get only one integer value so that I can just type in the value and press ENTER, so

  • the input field (if the solution includes a GUI) should automatically receive focus and
  • ENTER must initiate completion of input / send.

I cannot find a way to do this that satisfies both conditions, for example. ginput() from gWidgets activates the input field, but ENTER does not start submitting the form.

+4
source share
2 answers

This is how I solved my 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) 
+1
source

Update:

I can’t check it right now, but take a look at the ?menu and run the gui window.
I'm not sure if this will work, but it differs in that a mouse response is required.


original answer:

According to the documentation on ?readline :

This can only be used in an interactive session.
..
In non-interactive use, the result looks as if the response was RETURN, and the value is "".

If you just wait for one piece of information and don’t know this information before the script starts executing (presumably, you need to make a decision that depends on the results of the earlier script), then one alternative is to simply split your script into three parts:

  • everything to the point of decision.
  • interactive script that requests input
  • everything after the decision point.

And just connect the three together, having the first end, invoking the second in an interactive session. Then make the second end by calling the third.

+1
source

Source: https://habr.com/ru/post/1480646/


All Articles