[R] Timing issue using locator() in loop containing print()

Bob Meglen
Sun, 24 May 2009 19:24:31 -0000

I am attempting to use locator(n=2) to select the corners of several (5 in
this case) rectangles on an image displayed in a JavaGD window. The returned
coords are used to draw labeled rectangles around the selected region.  I
have tried several things to get this to work including sys.Sleep to correct
what appears to be a timing issue with this loop. The first-time print in
the loop doesn't print before locator executes several mouse clicks, and the
order of  pt(1) and pt(2) in each execution of the loop gets out of sync.
Please offer a suggestion.

I am using Windows, Java Gui for R1.6-3,  R version 2.8.1.

Example:

#..........................................................PLOT the Image in
Java
Window.........................................................................
JavaGD(name="JavaGD", width=640, height=480)
#.........................................suppress margins all
around.........................
par(mar=c(0,0,0,0))
image(xraw,col=my.grays(256),axes=F)
#....................................Set up
loop..........................................................................................................
tot_subsets<-5
ss<- matrix(0,nrow=tot_subsets,ncol=4)
print("begin selections")
# .........................Loop for multiple rectangle selections on
image.....................................................
for (i in 1:tot_subsets) {
print("Select lower left and upper right for this rectangle")

                  sub_0<- locator(n=2)


rect(sub_0$x[1],sub_0$y[1],sub_0$x[2],sub_0$y[2],col="red",density=0)

text(sub_0$x[1]+(sub_0$x[2]-sub_0$x[1])/2,sub_0$y[1]+(sub_0$y[2]-sub_0$y[1])/2,paste("S",i),col="red")
#.......................Add each reactangle coords to master subset
list.................................................
                          ss[i,1]<-sub_0$x[1]
                          ss[i,2]<-sub_0$y[1]
                          ss[i,3]<-sub_0$x[2]
                          ss[i,4]<-sub_0$y[2]
}
print("finished selection")



Thanks,
Bob Meglen
Boulder, CO