The Tabbed Notebook Widget

This example was kindly contributed by Dirk Eddelbuettel (with help from Peter Dalgaard).

The tabbed notebook widget is availabled as part of the Iwidgets package which does not come with the minimal Tcl/Tk installation bundled with R. If you want to use Iwidgets, install ActiveTcl and make sure that you have a TCL_LIBRARY environment variable set to your Tcl library path, e.g. C:\Tcl\lib\tcl8.4 and a MY_TCLTK environment variable set to a non-empty string, e.g. "Yes". If after doing this you still have problems with tclRequire("Iwidgets"), try addTclPath("C:\Tcl\lib").

The R code below creates the following widget:

Clicking on the "G" tab modifies the tkrplot graph. (The scale on the vertical axis indicates that the graph has been shifted up.)

Clicking on the "Z" tab modifies the tkrplot graph again.

R Code

## tabbed notebook

# inspired by http://incrtcl.sourceforge.net/iwidgets/iwidgets/tabnotebook.html
# and with help from Peter D.

library(tcltk)
library(tkrplot)
tclRequire("Iwidgets")

tt <- tktoplevel()
tkpack(tn <- tkwidget(tt, "iwidgets::tabnotebook"))
tkconfigure(tn,                         # prettyfication taken from incrtcl
            tabpos="n",
            width=650,
            height=350,
            angle=0,
            bevelamount=2,
            gap=2,
            margin=2,
            tabborders=0,
            tabbackground="white",
            background="lightgray",
            backdrop="lightgray")

nm <- LETTERS[1:26]
for (t in 1:length(nm)) {
    tbn <- tclvalue(tkadd(tn, label=nm[t]))
    tkpack(tbw <- .Tk.newwin(tbn))
    tkpack(fr <- tkframe(tbw))
    tkpack(lb <- tklabel(fr, text=paste("This is tab", nm[t])))
    ID <- paste(tn$ID, evalq(num.subwin<-num.subwin+1, tn$env), sep=".")
    win <- .Tk.newwin(ID)
    assign(ID, tbw, envir = tn$env)
    assign("parent", tn, envir = tbw$env)
    tkpack(tkrplot(fr, function() {
         X <- t + (1:20)
         plot(X)
    }))
    #list(tbw,fr,lb) # return all three in case you need them later
}
tkbind(tt, "<Destroy>", function() tkdestroy(tn))
tkselect(tn, 0)