solares@unsl.edu.ar
2003-Sep-09 15:10 UTC
[R] charge a vector with variables and to use as variable in a checkbutton?
hello, how i cant to charge in form dynamic a checkbutton, try to do it
with a vector be charged automaticamente but not
works, for example
library(tcltk)
tt<-tktoplevel()
f<-tkframe(tt)
tkpack(f)
i<-2
if (i==1) {b1<-tkcheckbutton
(f,text="b1",variable="b1",relief="raised");tkpack(b1);print(tclvalue
("b1"))}else if (i==2) {b1<-tkcheckbutton
(f,text="b1",variable="b1",relief="raised");tkpack(b1);print(tclvalue
("b1"));b2<-tkcheckbutton(f,text="b2",variable="b2",relief="raised");tkpack
(b2);print(tclvalue("b2"))}else if (i==3) {b1<-tkcheckbutton
(f,text="b1",variable="b1",relief="raised");tkpack(b1);print(tclvalue
("b1"));b2<-tkcheckbutton(f,text="b2",variable="b2",relief="raised");tkpack
(b2);print(tclvalue("b2"));b3<-tkcheckbutton
(f,text="b3",variable="b3",relief="raised");tkpack(b3);print(tclvalue
("b3"))}
i<-3
switch(i,{b1<-tkcheckbutton
(f,text="b1",variable="b1",relief="raised");tkpack(b1)},
{b1<-tkcheckbutton(f,text="b1",variable="b1",relief="raised");tkpack
(b1);b2<-tkcheckbutton(f,text="b2",variable="b2",relief="raised");tkpack
(b2)},
{b1<-tkcheckbutton(f,text="b1",variable="b1",relief="raised");tkpack
(b1);b2<-tkcheckbutton(f,text="b2",variable="b2",relief="raised");tkpack
(b2);;b3<-tkcheckbutton(f,text="b3",variable="b3",relief="raised");tkpack
(b3)})
as seeing I should charge in form estatic each checkbutton at to say the
value "i" because I cannot do somewhere as:
b1<-0;b2<-0;b3<-0
b<-c(b1,b2,b3) #vector b contain three variables
j<-3;i<-1
while (i<=j){
b[i]<-tkcheckbutton(f,text="b[i]",variable="b[i]",relief="raised")
#how b[i] is the same "variable" all the time for checkbutton
tkpack(b[i])
i<-i+1
}
Thanks Ruben
Dirk Eddelbuettel
2003-Sep-09 15:44 UTC
[R] charge a vector with variables and to use as variable in a checkbutton?
On Tue, Sep 09, 2003 at 12:10:06PM -0300, solares at unsl.edu.ar wrote:> hello, how i cant to charge in form dynamic a checkbutton, try to do it > with a vector be charged automaticamente but not > works, for example[...]> while (i<=j){ > b[i]<-tkcheckbutton(f,text="b[i]",variable="b[i]",relief="raised") > #how b[i] is the same "variable" all the time for checkbutton > tkpack(b[i]) > i<-i+1 > }You may want to re-read some of the available code examples. What you did is fundamentally broken -- you do not pass strings with variables names inside. You need to define some variables via the tclVar() function, and you can access the values of those variables in non-tcltk expressions with tclvalue(). What you want it probably close to this -- it uses menubuttons rather than checkbuttons but is the closest example I had hanging around here: color <- tclVar("blue") long.color <- tclVar(paste("Status is", tclvalue(color))) for (i in c("red", "green", "blue")) tkadd(m, "radio", label=i, variable=color, value=i, command=function(){ cat("Background is", tclvalue(color), "\n") tclvalue(long.color) <- paste("Status is", tclvalue(color)) }) Hth, Dirk -- Those are my principles, and if you don't like them... well, I have others. -- Groucho Marx