r-在Rstudio中单击后,我可以直接绘制每个locator()点吗



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

# 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

最新更新