Re: [R] tkbutton command - how to know which button was clicked?

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sat 01 Jul 2006 - 06:24:48 EST

JeeBee <JeeBee@troefpunt.nl> writes:

> In the below code fragment, print(arg) always prints the
> last element of rekeningen$rekening.
> Is this because of lazy evaluation? I.e. arg is evaluated at
> the time the button is pressed?

No and yes. Lazy evaluation has nothing to do with it, but the function passed to command= is not evaluated until the button is pressed. If you want a different "arg" variable for each button-function, you have to make sure that they have different environments or have the arg actually part of the function.

There are various possible variations. One is

mk.button <- function(arg)

     tkgrid(tkbutton(frame.1,
       text=paste("Saldo historie", arg),
       command=function() print(arg)),
       sticky="news")

for .... {

    ....
    mk.button(arg)
}

another is to wrap a local({arg <- arg;.....}) around the tkgrid call in your code. A 3rd one is

.....command=eval(substitute(function()print(arg)))...

> And, if so, how can I avoid this?
> I tried function() {force(arg); print(arg)} but that didn't work either.
>
> Thanks,
> Jeebee.
>
> for(rek in seq(1,nrow(rekeningen))) {
> arg <- rekeningen$rekening[rek]
>
> tkgrid(tkbutton(frame.1,
> text=paste("Saldo historie", arg),
> command=function() print(arg)),
> sticky="news")
> }

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sat Jul 01 09:51:15 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 01 Jul 2006 - 12:13:47 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.