I've used the code here: http://www.sciviews.org/_rgui/tcltk/Radiobuttons.html to ask a user question.
But R doesn't wait for the user to respond - the code continues to run, and so any subsequent code that tries to use the response is missing.
Here's my full code, I'd like testVal to be stored (it's the answer).
require(tcltk)
tt <- tktoplevel()
rb1 <- tkradiobutton(tt)
rb2 <- tkradiobutton(tt)
rb3 <- tkradiobutton(tt)
rb4 <- tkradiobutton(tt)
rb5 <- tkradiobutton(tt)
rbValue <- tclVar(NA)
tkconfigure(rb1,variable=rbValue,value=1)
tkconfigure(rb2,variable=rbValue,value=2)
tkconfigure(rb3,variable=rbValue,value=3)
tkconfigure(rb4,variable=rbValue,value=4)
tkconfigure(rb5,variable=rbValue,value=5)
tkgrid(tklabel(tt,text="What's your answer?"))
tkgrid(tklabel(tt,text="1"),rb1)
tkgrid(tklabel(tt,text="2"),rb2)
tkgrid(tklabel(tt,text="3"),rb3)
tkgrid(tklabel(tt,text="4"),rb4)
tkgrid(tklabel(tt,text="5"),rb5)
testVal <- NA
OnOK <- function()
{
rbVal <- tclvalue(rbValue)
tkdestroy(tt)
testVal <<- rbVal
print(rbVal)
#return(rbVal)
}
OK.but <- tkbutton(tt,text="OK",command=OnOK)
tkgrid(OK.but)
tkfocus(tt)
cat(testVal)
You could create an object done that is a tcl variable and use the function tkwait.variable to tell R to wait to proceed until that variable changes values.
Then incorporate a line in your function OnOK that changes the value of done. So,
## New object 'done'
done <- tclVar(0)
OnOK <- function()
{
rbVal <- tclvalue(rbValue)
tkdestroy(tt)
testVal <<- rbVal
## When OK button is pressed, value of 'done' is changed
tclvalue(done) <- 1
print(rbVal)
#return(rbVal)
}
OK.but <- tkbutton(tt,text="OK",command=OnOK)
tkgrid(OK.but)
tkfocus(tt)
## Tell R to wait for change in value of 'done'
tkwait.variable(done)
cat(testVal)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With