[R] Tck/tk help

Rick M rkmoreton at gmail.com
Mon Apr 12 22:38:21 CEST 2010


I am trying to create a simple window that displays a dataframe.  I was able
to put together a function from a couple of examples on the web.  For the
most part my function works.  The only problem is with resizing the window. 
I found a post where someone suggested using
with(env,tkpack("configure",etc.  ) but using tkpack messes around with the
layout obtained from tkgrid().  Any tips would be appreciated.

toTclArray<-function(dsn,dig=2) {

# Converts Data Frame/Matrix to a Tcl Array for Use in Displaying Tables
# dsn is the data set name
# dig is the number of digits to round to

	require(tcltk)
	tclarray1<-tclArray()

	for (i in 0:(dim(dsn)[1])) {
		for (j in 0:(dim(dsn)[2]-1)) {
			# First Row is Set to Column Names to be Used as Labels
			if (i==0) {
				tclarray1[[i,j]]<-colnames(dsn)[j+1] 
			} else {
				tem<-dsn[i,j+1]
				tclarray1[[i,j]]<-ifelse(is.na(tem),".",
					ifelse(is.numeric(tem),round(tem,digits=dig),
					as.character(tem)))
			}
		}
	}
	return (tclarray1)
}



displayInTable<-function(dsn,title="",height=-1,width=-1,dig=2,colwd=14)
{
	tclarray<-toTclArray(dsn,dig=dig)
	require(tcltk)
	tt<-tktoplevel()
	tclRequire("Tktable")
	tkwm.title(tt,title)
	table1<-tkwidget(tt,"table",rows=(dim(dsn)[1]+1),cols=dim(dsn)[2],
				titlerows=1,titlecols=0,colwidth=colwd,
				height=height+1,width=width+1,
				xscrollcommand=function(...) tkset(xscr,...),
				yscrollcommand=function(...) tkset(yscr,...) )

	xscr<-tkscrollbar(tt,orient="horizontal",command=function(...)
tkxview(table1,...) )
	yscr<-tkscrollbar(tt,command=function(...) tkxview(table1,...) )
	
	tkgrid(table1,yscr)
	tkgrid.configure(yscr,sticky="nsw")
	tkgrid(xscr,sticky="new")
	tkconfigure(table1,variable=tclarray,background="white",
		selectmode="extended",state="disabled")
	with(tt,{tkpack("configure",table1,expand=TRUE,fill="both")
		tkpack("configure",yscr,expand=TRUE,fill="y")
		tkpack("configure",xscr,expand=TRUE,fill="x")
	})
	return(table1)
}

# Example
try<-data.frame(matrix(1:100,5,20,byrow=TRUE))
colnames(try)<-1:20
displayInTable(try,title="1 to 100")
-- 
View this message in context: http://n4.nabble.com/Tck-tk-help-tp1837711p1837711.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list