0

I am using some commands which result in R command queries (from a server) that wait for keystrokes in the R-Studio Console (e.g.: Enter response: "), and I need to return strings as answers, one at a time. Thus, I need to post a response, wait about 10 seconds for the query to process and for the next query to post, and so on, and then let my code resume.

My guess is that this would be similar to posting an "a" to update all when you are installing a package -- or something like this.

Another issue is that I have the responses in lines inside an if statement, based on data sent to the online archive query, but the code inside the if statement just gets added to the cumulative command string with multiple "+" symbols on the left column. How can I enforce R to execute each line, and not jump ahead. This would be similar to c# and VB.NET for which non-threaded code doesn't get ahead of itself.

I believe I need to send keystrokes, like the the VB.NET's key command.

FYI - Print("Answer") does not work since the response is "Print("Answer")"

  • You need to provide some sort of [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) if you want help. The answer may vary depending on the functions involved. – MrFlick Sep 06 '16 at 23:48
  • Ok, here's the simplest example. Paste the command "chooseCRANmirror()" into an R syntax file (*.R) and then run the command. You will be prompted with a variety of servers for which you must manually enter a number in the Console after the "Selection:" prompt thrown by the server. What command can I use after "chooseCRANmirror()" to throw an "11" to the console? –  Sep 06 '16 at 23:51
  • Well, for me that opens a GUI window, then I click on an option -- no text input. And the interactive part can be avoided with an `option(repos=)` value. Again, it will depend on the function how to avoid the interactive prompt. – MrFlick Sep 06 '16 at 23:54
  • What command would throw the keystrokes "11" followed by an Enter to the Console? Thanks for shedding light on avoiding an interactive Gui; however, I need to throw keystrokes and an Enter command. –  Sep 07 '16 at 00:00
  • There is no such function in base R. And even if there were, base R only runs one function at a time so the function asking for input would block any other functions from running (including those trying to send input). You'd need to create a script outside of R to automate whatever GUI or console you are using. – MrFlick Sep 07 '16 at 00:05
  • 1
    `chooseCRANmirror()` uses the `menu` function to return the value of your selection, but I'm not sure this is what you're asking about. Are you looking for the `readline` function? It'll be helpful if you can give us a more tangible example (with some code) to help fix ideas. – Weihuang Wong Sep 07 '16 at 03:16

1 Answers1

0

Hope this helps:

Source ("Eleven.r"):

A <- readline ( "What is 42 minus 31?")
lapply (1:100,cat, x=A, sep = " - ")

Calling script:

source ("Eleven.r")
11
sys.sleep ( 5 )
David.
  • 132
  • 6