[R] locator() via tcltk

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jun 6 08:46:18 CEST 2005


On Sun, 5 Jun 2005, Sebastian Luque wrote:

> 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 ensures the window is displayed as a window and not an icon, and on 
Windows that it gets raised.  It may not be necessary, but it is a safety 
measure.

> 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.

On R for Windows, in R_HOME/Tcl/doc.
On a Unix-alike, use the man pages.

Unfortunately, you need to be able to work out that tkwm.deiconify maps 
to 'wm deiconify' which means you need the help/man page for 'wm' which 
says

wm deiconify window

Arrange for window to be displayed in normal (non-iconified) form. This is 
done by mapping the window.  If the window has never been mapped then this 
command will not map the window, but it will ensure that when the window 
is first mapped it will be displayed in de-iconified form.  On Windows, a 
deiconified window will also be raised and be given the focus (made the 
active window). Returns an empty string.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list