首页 > 解决方案 > 单击 Rstudio 后,我可以直接绘制每个 locator() 点吗?

问题描述

plot(1:10) ; locator(3, type="p")在外部图形设备中工作正常。
Rstudio 仅在定位完成后绘制点。
如何在每次点击后立即绘图?

标签: rplotrstudio

解决方案


# 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

推荐阅读