Michael Lawrence
Fri, 26 Feb 2010 05:55:57 -0800
On Fri, Feb 26, 2010 at 5:15 AM, Liviu Andronic <landronim...@gmail.com>wrote:
> Dear all
> I am trying to create several sliders, each in its own container and with
> its own handler. It works fine when I create all the widgets manually, but
> gives some strange behaviour when I pass everything through a loop.
>
> ## function to be used by the handler of the slider
> count.mv.int <- function(x, start=1, end=2){
> assign("i", x, envir = .GlobalEnv)
> print(paste("i = ", i))
> }
> count.mv.int(2); i
>
> ## function to be used by the handler of the slider
> count.mv.let <- function(x, start=1, end=4){
> assign("l", letters[x], envir = .GlobalEnv)
> print(paste("l = ", l))
> }
> count.mv.let(3); l
>
> ## manually created widgets
> count.pop <- function(fun=c("count.mv.int", "count.mv.let")){
> require(gWidgets)
> options(guiToolkit="tcltk")
> w <- gwindow(paste("Counters"))
> g <- ggroup(cont=w, horizontal=T)
> g1 <- ggroup(cont=g, horizontal=FALSE)
> glabel(paste("'", fun[1], "'", sep=""), cont=g1)
> h1 <- function(h,...) eval(parse(text=paste(fun[1], "(svalue(h$obj))",
> sep="")))
> s1 <- gslider(from=formals(fun[1])$start, to=formals(fun[1])$end,
> by=1, cont=g1, handler=h1)
> gseparator(horizontal=FALSE, cont=g, expand=TRUE)
> g2 <- ggroup(cont=g, horizontal=FALSE)
> glabel(paste("'", fun[2], "'", sep=""), cont=g2)
> h2 <- function(h,...) eval(parse(text=paste(fun[2], "(svalue(h$obj))",
> sep="")))
> s2 <- gslider(from=formals(fun[2])$start, to=formals(fun[2])$end,
> by=1, cont=g2, handler=h2)
> }
> ## works fine: each slider change will trigger the correct handler
> count.pop()
>
> ## widgets created through loops
> count.pop1 <- function(fun=c("count.mv.int", "count.mv.let")){
> require(gWidgets)
> options(guiToolkit="tcltk")
> w <- gwindow(paste("Counters (loop)"))
> g <- ggroup(cont=w, horizontal=T)
> for(k in 1:length(fun)){
> x.cont <- paste("g", i, sep="")
> assign(x.cont, ggroup(cont=g, horizontal=FALSE))
> glabel(paste("'", fun[k], "'", sep=""), cont=get(x.cont))
> x.handl <- paste("h", i, sep="")
> assign(x.handl, function(h,...) eval(parse(text=paste(fun[k],
> "(svalue(h$obj))", sep=""))))
> x.sl <- paste("s", i, sep="")
> assign(x.sl, gslider(from=formals(fun[k])$start,
> to=formals(fun[k])$end,
> by=1, cont=get(x.cont), handler=get(x.handl)))
> gseparator(horizontal=FALSE, cont=g, expand=TRUE)
> }
> }
> ## wrong behaviour: both sliders will trigger the second handler only
> count.pop1()
>
> Could someone hint at what I am doing wrong? Thank you
> Liviu
>
>
You probably want to change the for() loop to an lapply construct, so that
you are creating a unique closure around each handler. Otherwise, they'll
both see the same value of 'k' (whatever it was set to last).
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-GUI mailing list
> R-SIG-GUI@stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-gui
>
[[alternative HTML version deleted]]
_______________________________________________
R-SIG-GUI mailing list
R-SIG-GUI@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-gui