Show points from locator() as they are selected

I want to use locator() to place user-selected points on a plot. In the RStudio plot pane a small "blue pin" shows when I select a point, but no mark is left at that point until I stop locator(). [While not that important the locator beep is also not issued when the plot is in the RStudio plot pane.] This can be seen when running the following in RStudio (I commented the lines as locator() is an interactive function).

# plot(1)
# locator(2,type="p")

However, if a new window is opened (separate from RStudio) then points are added to the plot as they are selected (and the locator beep is issued). This may be seen with the following.

# windows(5,5)
# plot(1)
# locator(2,type="p")

Is it possible to have the points appear as they are selected when using the RStudio plot pane?

I hadn't used locator before, but just ran it following the instructions here:
It seems to me that locator() is just selecting the coordinates, and it's only afterward that you add the geometry that anything shows up.

Is this different from how it works for you if you run it in R outside of RStudio?


Yes, the behavior is different whether run in R or RStudio. The first (two-line) example in my original post will not show the selected points until after stopping locator() when using the RStudio plot pane, but will show the points immediately after clicking when run in R.

The video you linked to does not show the points even after stopping locator() because the author did not include type="p" in locator().

My use case is that users of a function that I am writing will need to select several points and it would be easier on them if they could see where they have already selected. To my knowledge thusfar, this is not possible when using the RStudio plot pane (but would be if just using R or using a separate plot window from within RStudio).

Got it. What versions of R and RStudio are you running, respectively?
I won't know the fix, regardless, but helpful info to have for someone who does! :+1:

Thanks and good point.

I am using R v3.4.3 and RStudio v1.1.423 on a Windows machine.

1 Like

Hi dogle,

I ran into the same problem and since there was no solution here I had to come up with my own. The following code will do what you are asking:

plot.locator<-function(, num.points){
for( I in 1:num.points){
# This message tells the user what to do next
message("Select next point")

# I used filled points that are blue
location<-locator(1,type="p", par(pch=16,col="blue"))
locations<-list(x=xs.list, y =ys.list)

# use the function to return 4 points selected from a plot item called cool.plot:

Hopefully that helps. Not an ideal solution but it works.

Thanks drywdragon. I solved my problem by moving in a completely different direction. However, thank you for your reply.

Great! Can you share what your solution was? Could be helpful to others who find this thread. I'm curious as well.

1 Like

I changed to using getGraphicsEvent() from grDevices and forcing the user to use a separate plotting window rather than the plot pane in RStudio (also because I need to control the size and aspect ratio of the window and I wanted the user to be able to delete a point). My actual function is complicated (go to line 124 here for the primary function, but it largely stemmed from these blog pots: here and here.