[R-gui] embedding R graphics in Tk widgets

Wayne.W.Jones at shell.com Wayne.W.Jones at shell.com
Fri Jun 13 09:37:32 CEST 2008


Hi Sebastian, 

The package rpanel (see www.stats.gla.ac.uk/~adrian/rpanel/) makes it really easy to embed plots into tcltk widgets. 


Try the following in R: 

#----------------------------------------------------------------------------------------
install.packages("rpanel")
require(tkrplot)
library(rpanel)
my.action<-function(panel,x,y){print(c(x,y)); return(panel)}
rpplot <- rp.control(title = "Demonstration of rp.tkrplot", h = 1)

redraw <- function(panel) {
       rp.tkrreplot(panel, tkrp)
}

rp.tkrplot(rpplot, tkrp, function(panel) plot(1:20, (1:20)^panel$h),action=my.action)
rp.slider(rpplot, h, action = redraw, from = 0.05, to = 2.00, resolution = 0.05)
#----------------------------------------------------------------------------------------


When you left click on the plot the function "my.action" prints the x,y pixel coordinates of the point you clicked in the graph. I have written functions in the past which map the pixel coordinates to the grpahical user coords. 
Then it picks out the nearest point. 

This is still slightly messy though! It is not possible to generalise the process of mapping pixel coordinates with the graphical user coords. It can depend on the settings of the users PC etc. 

The developers of rpanel are aware of this and may, in the future, build a locator function in the rpanel package. 

I hope this helps. 

Regards

Wayne











-----Original Message-----
From: r-sig-gui-bounces at stat.math.ethz.ch
[mailto:r-sig-gui-bounces at stat.math.ethz.ch]On Behalf Of Sebastian P.
Luque
Sent: 13 June 2008 04:14
To: r-sig-gui at stat.math.ethz.ch
Subject: [R-gui] embedding R graphics in Tk widgets


Hi,

Several years ago I was looking for ways to embed R graphics into Tk
widgets, and abandoned the idea after finding out it was way too
difficult, at least for my purposes.  More recently, I looked again and
found Luke Tierney's trkplot, which allows one to do just this.
However, there seem to be limitations like inability to resize the
graphs along with the widgets.  That's not a big encumbrance in my case,
but the difficulty in accessing the R plot's coordinates is.  The only
guidance I could find was that in James Wettenhall:

http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/InteractiveTkrPlot.html

but it issues an enigmatic message for the calculated coordinates:

"Label Point Closest to These Approximate Plot Coordinates"

Is that the best approach available, or are there better alternatives?

My idea is to implement an integrated interface for a plotting utility I
wrote for package diveMove, which gathers pairs of coordinates from
mouse clicks (in standard unembedded graphs for now):

---<---------------cut here---------------start-------------->---
install.packages("diveMove")  # from any CRAN
data(divesTDR)
depth.zoc <- zoc(getTime(divesTDR), getDepth(divesTDR), offset=3)
coords <- plotTD(getTime(divesTDR), depth.zoc)
---<---------------cut here---------------end---------------->---

Clicking on the "Zero-Offset Correct a Range" button, followed by a pair
of clicks in the graph, gathers coordinates using locator().  Because
users may need to zoom and pan across different scales, it is important
to allow for resizing of the plot.

Any recommendations on how to embed R graphics on Tk widgets, and
gathering coordinates as above, would be much appreciated.  Thanks.


Cheers,

-- 
Seb

_______________________________________________
R-SIG-GUI mailing list
R-SIG-GUI at stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-gui



More information about the R-SIG-GUI mailing list