Using the Tk Table widget in R TclTk

A Short Example

The TkTable widget is a very sophisticated spreadsheet-like widget which can display tables or allow the user to enter data in a tabular format. Firstly, a short example, using the new tclArray() function which was introduced in R 1.8.0

require(tcltk)
tclRequire("Tktable")

myRarray <- c("Name","\"James Wettenhall\"","R-Help",
              "Email","wettenhall@wehi.edu.au","R-Help@stat.math.ethz.ch")
dim(myRarray) <- c(3,2)

tclarray <- tclArray()

for (i in (0:2))
  for (j in (0:1))
     tclarray[[i,j]] <- myRarray[i+1,j+1]

tt<-tktoplevel()
table1 <- tkwidget(tt,"table",variable=tclarray,rows=3,cols=2,titlerows=1,selectmode="extended",colwidth=25,background="white")
tkpack(table1)

Running the R code above gives the following window:


A More Detailed Example

The next example displays a large matrix in a table with scrollbars, and demonstrates how to modify a value within the table.

displayInTable <- function(tclarray,title="",height=-1,width=-1,nrow=-1,ncol=-1)
{
  require(tcltk)
  tt <- tktoplevel()
  tclRequire("Tktable")
  tkwm.title(tt,title)
  table1 <- tkwidget(tt,"table",rows=nrow,cols=ncol,titlerows=0,titlecols=0,
                     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(...)tkyview(table1,...))

  tkgrid(table1,yscr)
  tkgrid.configure(yscr,sticky="nsw")
  tkgrid(xscr,sticky="new")
  tkconfigure(table1,variable=tclarray,background="white",selectmode="extended")
  return (table1)
}


# Define a matrix :
matrix1 <- matrix(1:2000,nrow=50,ncol=40)

# Define a Tcl array and initialize it to that matrix :
require(tcltk)
tclArray1 <- tclArray()
for (i in (1:50))
  for (j in (1:40))
    tclArray1[[i-1,j-1]] <- matrix1[i,j]

table1 <- displayInTable(tclArray1,nrow=50,ncol=40)


# Display the Tcl array, showing only 10 rows and 10 columns :
table1 <- displayInTable(tclArray1,height=10,width=10,nrow=50,ncol=40)


# Change the value of one of the elements in the Tcl array :
tclArray1[[2,2]] <- 999999


> tclArray1[[2,2]]
<Tcl> 999999 
> tclvalue(tclArray1[[2,2]])
[1] "999999"

Additional Notes

Copying to External Spreadsheet Programs

To allow copying from a table widget and pasting into a spreadsheet program such as Excel, use :

tkconfigure(table1,selectmode="extended",rowseparator="\"\n\"",colseparator="\"\t\"")

To control whether rows and/or columns can be resized, use :

tkconfigure(table1,resizeborders="none")    # OR
tkconfigure(table1,resizeborders="both")    # OR
tkconfigure(table1,resizeborders="row")     # OR
tkconfigure(table1,resizeborders="col")

Line-Wrapping Within Cells

To prevent line-wrapping within cells, use:

tkconfigure(table1,multiline=0)

Adding/Inserting Rows and Columns

To add a row at the end of the table, use:

tkinsert(table1,"rows","end",1)

To add a column at the end of the table, use:

tkinsert(table1,"cols","end",1)

To insert a row before the current row, use:

tkinsert(table1,"rows",tclvalue(tkindex(table1,"active","row")),-1)

(The negative sign means insert before the current row, not after.)

To insert a columnm before the current column, use:

tkinsert(table1,"cols",tclvalue(tkindex(table1,"active","col")),-1)

Deleting Rows and Columns

To delete a row at the end of the table, use:

tkdelete(table1,"rows","end",1)

To delete a column at the end of the table, use:

tkdelete(table1,"cols","end",1)

To delete the currnet row, use:

tkdelete(table1,"rows",tclvalue(tkindex(table1,"active","row")),1)

To delete the current columnm, use:

tkdelete(table1,"cols",tclvalue(tkindex(table1,"active","col")),1)