[R] Retrieving data from a tcl /tk function

Judith Flores juryef at yahoo.com
Sat Jul 19 01:13:41 CEST 2008


Hello,

   I am sorry if this is an answered question, but I did my homework for a long while and couldn't figure out a way to retrieve data entry from a model dialog. In one of the examples compiled by James Wettenhall:

odalDialog <- function(title, question, entryInit, entryWidth = 20,
    returnValOnCancel = "ID_CANCEL") {
    dlg <- tktoplevel()
    tkwm.deiconify(dlg)
    tkgrab.set(dlg)
    tkfocus(dlg)
    tkwm.title(dlg, title)
    textEntryVarTcl <- tclVar(paste(entryInit))
    textEntryWidget <- tkentry(dlg, width = paste(entryWidth),
        textvariable = textEntryVarTcl)
    tkgrid(tklabel(dlg, text = "       "))
    tkgrid(tklabel(dlg, text = question), textEntryWidget)
    tkgrid(tklabel(dlg, text = "       "))
    ReturnVal <- returnValOnCancel

    onOK <- function() {
        ReturnVal <<- tclvalue(textEntryVarTcl)
        tkgrab.release(dlg)
        tkdestroy(dlg)
        tkfocus(ttMain)
    }
    onCancel <- function() {
        ReturnVal <<- returnValOnCancel
        tkgrab.release(dlg)
        tkdestroy(dlg)
        tkfocus(ttMain)
    }
    OK.but <- tkbutton(dlg, text = "   OK   ", command = onOK)
    Cancel.but <- tkbutton(dlg, text = " Cancel ", command = onCancel)
    tkgrid(OK.but, Cancel.but)
    tkgrid(tklabel(dlg, text = "    "))

    tkfocus(dlg)
    tkbind(dlg, "<Destroy>", function() {tkgrab.release(dlg); tkfocus(ttMain)})
    tkbind(textEntryWidget, "<Return>", onOK)
    tkwait.window(dlg)

    return(ReturnVal)
}

# Create a "main" window with a button which activates our dialog
require(tcltk)
ttMain <- tktoplevel()
tktitle(ttMain) <- "ttMain"
launchDialog <- function() {
    ReturnVal <- modalDialog("First Name Entry", "Enter Your First Name", "")
    if (ReturnVal == "ID_CANCEL") return()
    tkmessageBox(title = "Greeting",
        message = paste("Hello, ", ReturnVal, ".", sep = ""))
}
launchDlg.button <- tkbutton(ttMain, text = "Launch Dialog", command = launchDialog)
tkpack(launchDlg.button)


      How can I retrieve the input value of ReturnVal? Also, is there a book that you would recommend to understand the interface between tcl/tk and R?  Dr. Dalgaard's article was very helpful, but wondering if there is more information on the topic.


Thank you for your help,



More information about the R-help mailing list