Hello, I'm trying to write a function using tcltk to interactively modify a plot and gather locator() data. I've read Peter's articles in Rnews, the help pages in tcltk, http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/, plus a post in R-help sometime ago, but haven't found a solution. The idea goes something like this: require(tcltk) testplot <- function() { getcoords <- function(...) { locator(5) } x <- 1:1000 y <- rnorm(1000) plot(x, y) base <- tktoplevel() loc.pts <- tkbutton(base, text = "Get coordinates", command = getcoords) quit.but <- tkbutton(base, text = "Quit", command = function() tkdestroy(base)) tkpack(loc.pts, quit.but) } I'd like testplot to return the value from getcoords. The "Get coordinates" button seems to be correctly calling getcoords, and locator is doing its job, but I don't know how to store its value. Any help is very much appreciated. Regards, Sebastian -- Sebastian P. Luque
Sebastian Luque wrote:> Hello, > > I'm trying to write a function using tcltk to interactively modify a plot > and gather locator() data. I've read Peter's articles in Rnews, the help > pages in tcltk, http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/, > plus a post in R-help sometime ago, but haven't found a solution. > The idea goes something like this: > > require(tcltk) > testplot <- function() { > getcoords <- function(...) { > locator(5) > } > x <- 1:1000 > y <- rnorm(1000) > plot(x, y) > base <- tktoplevel() > loc.pts <- tkbutton(base, text = "Get coordinates", command = getcoords) > quit.but <- tkbutton(base, text = "Quit", > command = function() tkdestroy(base)) > tkpack(loc.pts, quit.but) > } > > I'd like testplot to return the value from getcoords. The "Get > coordinates" button seems to be correctly calling getcoords, and locator > is doing its job, but I don't know how to store its value. Any help is > very much appreciated.You can do this with local functions. For example, testplot <- function() { coords <- NA getcoords <- function(...) { coords <<- locator(5) } x <- 1:1000 y <- rnorm(1000) plot(x, y) base <- tktoplevel() loc.pts <- tkbutton(base, text = "Get coordinates", command = getcoords) quit.but <- tkbutton(base, text = "Quit", command = function() tkdestroy(base)) tkpack(loc.pts, quit.but) return(function() coords) } This stores the results in a variable named coords in the testplot frame. testplot itself returns a function that can read that value. You'd use it like this: lastval <- testplot() ## do some button clicking... lastval() # will print the result of the last click. This is one of the somewhat weird but very nice features of R. S-PLUS doesn't work this way. Duncan Murdoch
You have two asynchronous processes here: your R function will return, leaving a Tk widget up. Duncan Murdoch's solution is to give you a function that will retrieve at some future stage the result of the last press (if any) of "Get coordinates" button. Is that what you want? 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. On Fri, 3 Jun 2005, Sebastian Luque wrote:> Hello, > > I'm trying to write a function using tcltk to interactively modify a plot > and gather locator() data. I've read Peter's articles in Rnews, the help > pages in tcltk, http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/, > plus a post in R-help sometime ago, but haven't found a solution. > The idea goes something like this: > > require(tcltk) > testplot <- function() { > getcoords <- function(...) { > locator(5) > } > x <- 1:1000 > y <- rnorm(1000) > plot(x, y) > base <- tktoplevel() > loc.pts <- tkbutton(base, text = "Get coordinates", command = getcoords) > quit.but <- tkbutton(base, text = "Quit", > command = function() tkdestroy(base)) > tkpack(loc.pts, quit.but) > } > > I'd like testplot to return the value from getcoords. The "Get > coordinates" button seems to be correctly calling getcoords, and locator > is doing its job, but I don't know how to store its value. Any help is > very much appreciated.-- 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