Skip to content

[R-gui] Drawing on embedded RGtk2 plotting area with Cairo

2 messages · N K, Michael Lawrence

N K
#
Hi,

I'm trying to draw some crosshairs and floating X,Y coordinates in a 
text box that follow the mouse cursor around on an embedded R plotting 
area (asCairoDevice).     i.e., I want to have Cairo line and text 
objects that are updated whenever the mouse moves over an R plot.

I used the commands below but the text flickers, like it is being erased 
immediately after being drawn.      Has anyone solved this problem or 
have some pointers, demos, examples, etc?      Is there some limitation 
with the R Cairo device that prevents user interaction like this?    
Before calling asCairoDevice I can draw on the gtk drawing area, but not 
after.      Any help appreciated!

plotArea <- gtkDrawingArea()
gtkWidgetSetSizeRequest(plotArea, 300, 400)
#  Receive mouse events....
gtkWidgetAddEvents(plotArea, 2)
asCairoDevice(plotArea)
gtkWidgetShow(plotArea)
plotArea$AddCallback("motion-notify-event", function(w,ev) {
         x <- ev$x
         y <- ev$y
         cr$moveTo(x,y)
         coor <- win2plot(x, y)   #  Note this converts from screen to 
user coordinates
         cr$showText(paste(coor[1],coor[2], sep=", "))
})
cr <- gdkCairoCreate(plotArea$window)
plot(1:10)

--Neil