[R] Tktable change cell values

James Wettenhall wettenhall at wehi.edu.au
Tue Jul 29 02:57:05 CEST 2003


Thomas Sudler <TSudler at ch.imshealth.com> wrote:
> And my last question: How can I change the value of a cell? (I 
mean not by
> clicking into the cell... I mean how I can change the value 
with a
> command).

Thomas,

Currently there is no official interface between R arrays and 
Tcl arrays, so you have to write your own.

I've sketched some ideas on :
http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/tktable.html

Here's a summary.

Regards,
James

The tclArrayVar is a modified version of Peter Dalgaard's 
tclVar function.

require(tcltk)
tclRequire("Tktable")

tclArrayVar <- function()
{
    n <- evalq(TclVarCount <- TclVarCount + 1, .TkRoot$env)
    name <- paste("::RTcl", n,sep = "")
    l <- list(env = new.env())
    assign(name, NULL, envir = l$env)
    reg.finalizer(l$env, function(env) tkcmd("unset", ls(env)))
    class(l) <- "tclArrayVar"
    .Tcl(paste("set ",name,"(0,0) \"\"",sep=""))
    l
}

assign("[.tclArrayVar",
function(object, i, j) {
  tclArrayName <- ls(object$env)
  tclvalue(paste(tclArrayName,"(",i,",",j,")",sep=""))
})

assign("[<-.tclArrayVar",
function(object, i,j,value) {
  tclArrayName <- ls(object$env)
  .Tcl(paste("set ",tclArrayName,"(",i,",",j,") ",value,sep=""))  
  return(object)
})

tt <- tktoplevel()
tclArray <- tclArrayVar()
tclArrayName <- ls(tclArray$env)
table1 <- tkwidget(tt,"table",variable=tclArrayName)
tkpack(table1)

# Set/Modify the values of some cells.
tclArray[0,0] <- "Zero"
tclArray[0,1] <- "One"
tclArray[1,0] <- "Two"
tclArray[1,1] <- "Three"

# Now get the value of a cell.
tclArray[0,1]




More information about the R-help mailing list