Search code examples
rmouseeventwait

How do I make an r-script wait for a mouse click?


I have this simple script that I execute from command line (c:\path\foo> r --ess --slave -q -f myScript.R):

library(ggmap)

x11()
qmap('Hauptbahnhof Zürich', zoom=18, source='osm')

I want the script to wait until the user clicks with the mouse into the image. This version just exits.

I tried to use z <- locator(1), yet this gives me a Error in locator(1) : plot.new has not been called yet Execution halted error.

I also tried

getGraphicsEvent(
    prompt      = "Waiting for input", 
    onMouseDown = NULL,
    onMouseMove = NULL,
    onMouseUp   = NULL, 
    onKeybd     = NULL)

yet, this approach just print NULL to the console and then exits.

So, how would I go about waiting for the user to click into the image to proceed further?


Solution

  • While I didn't find an answer to make the script wait until the user clicks with the mouse, at least I can wait for him to press enter on the console:

    readLines('stdin', n=1)