[R] locator() via tcltk

Sebastian Luque spluque at gmail.com
Sun Jun 5 20:49:18 CEST 2005


Dear List members,

Thank you so much for your insights.


On Sat, 4 Jun 2005 09:39:33 +0100 (BST),
Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:

[...]

> I think it is more likely you want to wait for the Tk interaction and
> then return the results, that is use a `modal' widget. If so, take a
> look at the examples in src/library/tcltk/R/utils.R which are modal and
> return their results.


Yes, this is indeed what I was looking for. Additionally, I needed to be
able to make several calls to getcoords and store the results from each
call, i.e.: be able to repeat the sequence 1. click "Get coordinates", 2.
do some clicks, finish by clicking any button other than left, as many
times as needed, and return the results from each sequence separately. I
looked at the code you pointed me to and came up with this:

require(tcltk)

testplot <- function() {
  coords <- list()
  getcoords <- function(...) {
  coords[[length(coords) + 1]] <<- locator()
  tkgrab.release(base)
  }
  x <- 1:1000
  y <- rnorm(1000)
  plot(x, y)
  base <- tktoplevel()
  tkwm.deiconify(base)
  tkgrab.set(base)
  tkfocus(base)
  loc.pts <- tkbutton(base, text = "Get coordinates", command = getcoords)
  quit.but <- tkbutton(base, text = "Quit",
                       command = function() tkdestroy(base))
  tkpack(loc.pts, quit.but)
  tkwait.window(base)
  return(coords)
}

testplot()

## Do a few sessions of coordinate gathering, and press "Quit". The result
## is a list with the coordinates from each session.

This is exactly the idea I needed! However, I don't understand what
tkwm.deiconify() does there. It would help to know where the full
documentation for all tcltk functions is. The help pages suggest to
consult the tcltk documentation, but I don't know where this is.

Thank you,
Sebastian
-- 
Sebastian P. Luque




More information about the R-help mailing list