Downloading and installing the Bwidget library
1 Using widgets from the Bwidget library
One of the handiest widgets available is the "Tree" widget,
which shows the upper level of a hierarchy of objects, and
which displays the lower levels of the hierarchy when the
appropriate graphic element is clicked. Here's an illustration
of the tree widget displaying some data frames; the wine
data frame's variables are displayed, since the plus sign to the
left of its name was clicked.
The Bwidget library is not included with the version of Tcl that
comes with R under Windows, but it is very easy to install because
it is "pure" Tk; in other words, no programs have to be compiled
in order to install the library. To install the Bwidget library
under Windows, follow these steps:
- Go to http://tcl.activestate.com/software/tcllib/
-
Click on the link labeled "Download distributions"
-
Click on the "Download" link for "BWidget".
-
Click on the link labeled "BWidget-1.7.0.zip", and follow the instructions
to download this file to your computer.
-
Find the location of the Tcl libraries that were installed as part of R.
This will probably be c:\Program Files\R\R-2.2.1\Tcl\lib, but may
vary if you're using a different version of R. In any event, you want to
find the lib directory in the Tcl folder.
-
Extract the files contained in the zip file into the folder you found in the
previous step. Once this is done, the tree widget should be available.
Here's the program that uses the tree widget:
require(tcltk)
tclRequire("BWidget")
doit = function(...){
use = tclvalue(tkcmd(treeWidget,"selection","get"))
parts = strsplit(use,':')[[1]]
df = get(parts[1])
var = df[[parts[2]]]
title = paste(parts,compress='$')
type = tclvalue(type_)
funs = list(hist = function(x,title)hist(x,main=title),
box = function(x,title)boxplot(x,main=title),
dens = function(x,title)plot(density(x,na.rm=TRUE),type='l',main=title))
funs[[type]](var,title)
}
main = tktoplevel()
tkwm.title(main,"Tree (Drill-Down) Widget")
dffr = tkframe(main)
xScr = tkscrollbar(dffr,command=function(...)tkxview(treeWidget,...),orient="horizontal")
yScr = tkscrollbar(dffr,command=function(...)tkyview(treeWidget,...))
treeWidget = tkwidget(dffr,"Tree",xscrollcommand=function(...)tkset(xScr,...),
yscrollcommand=function(...)tkset(yScr,...),width=30,height=15)
tkpack(yScr,side='right',fill='y')
tkpack(xScr,side='bottom',fill='x')
tkpack(treeWidget,fill='both',expand=1)
tkpack(dffr)
oo = objects()
use = oo[sapply(oo,function(x)class(get(x))) == 'data.frame']
for(i in use){
tkinsert(treeWidget,"end","root",i,text=i)
nms = names(get(i))
classes = sapply(get(i),class)
modes = sapply(get(i),mode)
wh = (classes != 'factor' & modes == 'numeric')
y = nms[wh]
for(j in y)tkinsert(treeWidget,"end",i,paste(i,j,sep=":"),text=j)
}
chfr = tkframe(main)
type_ = tclVar()
f1 = tkframe(chfr)
tkpack(tklabel(f1,text='Hist',width=8),side='left')
tkpack(tkradiobutton(f1,variable=type_,value='hist'),side='left')
f2 = tkframe(chfr)
tkpack(tklabel(f2,text='Box',width=8),side='left')
tkpack(tkradiobutton(f2,variable=type_,value='box'),side='left')
f3 = tkframe(chfr)
tkpack(tklabel(f3,text='Dens',width=8),side='left')
tkpack(tkradiobutton(f3,variable=type_,value='dens'),side='left')
tkpack(f1,side='left')
tkpack(f2,side='left')
tkpack(f3,side='left')
tkpack(chfr,side='top')
tkpack(tkbutton(main,command=doit,text='Plot'),side='bottom')
When elements are inserted in the tree with tkinsert, three
additional arguments have to be provided. The first tells where the
item will be inserted. Using a value of ënd" is usually
appropriate. The second represents an identifier for the place within
the hierarchy that this item should be inserted.
If it's a toplevel entry (like the data frame names
in this example), the "root" identifier is used.
The third argument represents the identifier for the element being
inserted. All of these identifiers must be unique, and it is this
third argument that will be returned to identify which element of the
tree has been selected. In this example, I use the data frame names
as the toplevel components, and the data frame name and variable name
separated by a colon as the lower level identifiers.
File translated from
TEX
by
TTH,
version 3.67.
On 3 May 2006, 12:13.