Berry Boessenkool
Berry Boessenkool

Reputation: 1538

Can I plot each locator() point directly after clicking in Rstudio?

plot(1:10) ; locator(3, type="p") works fine in an external graphics device.
Rstudio only plots the points after locating finishes.
How can I plot right after each click?

Upvotes: 2

Views: 208

Answers (1)

Berry Boessenkool
Berry Boessenkool

Reputation: 1538

# A workaround is the following:
rslocator <- function(n=512, type="p", ...)
 {
 on.exit(return(list(x=x,y=y))) # output even when function is canceled with ESC in console
 x <- y <- NULL
 i <- 1
 while(i<=n)
   {
   d <- locator(1)
   if(is.null(d)) break # If user pressed ESC in Rstudio Graphics window
   x <- c(x, d$x)
   y <- c(y, d$y)
   points(x,y, type=type, ...)
   i <- i+1
   }
 }


plot(1:10, type="n")
rslocator(7, col="blue", type="o")
# plots each point right after clicking and ESC still works fine

Upvotes: 2

Related Questions