[R-gui] gWidgets: multiple handlers using a loop?

Liviu Andronic landronimirc at gmail.com
Fri Feb 26 15:37:57 CET 2010


On 2/26/10, Michael Lawrence <lawrence.michael at gene.com> wrote:
> 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).
>
Indeed, thanks! On quick check, the following does what I originally intended.
Liviu

count.pop3 <- function(fun=c("count.mv.int", "count.mv.let")){
    require(gWidgets)
    options(guiToolkit="tcltk")
    w <- gwindow(paste("Counters (lapply)"))
    g <- ggroup(cont=w, horizontal=T)
    x.fun <- function(k){
        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)
    }
    lapply(1:length(fun) , x.fun)
}
count.pop3()



More information about the R-SIG-GUI mailing list