0
require(tcltk)  
ttMain <- tktoplevel()  
tktitle(ttMain) <- "ttMain"  
launchDialog <- function() {  
    ReturnVal <- modalDialog("First Gene", "Enter A Gene Name", "")  
    if (ReturnVal == "ID_CANCEL") return()  
    tkmessageBox(title = "Heatmap",  
        message = paste("Hello, ", ReturnVal, ".", sep = ""))  
}   
launchDlg.button <- tkbutton(ttMain, text = "Launch Dialog", command = launchDialog)  
tkpack(launchDlg.button)

I want to rewrite the last line of the code to have the message return a heatmap. I have a dataframe with all the data necessary (data about gene expression in numerical form), called pedM, and ReturnVal represents a column name (a particular gene) within that dataframe. Please help.

Any tips that can be provided would be amazing. Thanks in advance.

3
  • What language is this? It sure doesn't look like Tcl… Aug 6, 2012 at 21:08
  • Sorry, should have clarified. This is in R using a Tcl/tk package. Aug 6, 2012 at 21:11
  • looks like a job for tkrplot to me.
    – Karsten W.
    Aug 7, 2012 at 0:45

1 Answer 1

0

Here is an example that may help. Your code uses a modalDialog function that AFAIK does not exist. Here is an example of how to roll your own

library(tcltk)  
library(tcltk2)

tkinput <- function(parent, title, label, okButLabel="Ok", posx=NULL, posy=NULL) {
  if(!require(tcltk2)) stop("This function requires the package tcltk2.")
  if(!require(tcltk)) stop("This function requires the package tcltk.")

  # param checks
  if(!is.character(title)) stop("invalid title argument - character required.")
  if(!is.character(label)) stop("invalid label argument - character required.")

  # toplevel
  tclServiceMode(FALSE) # don't display until complete
  win <- tktoplevel(parent)
  #win <- .Tk.subwin(parent)
  tkwm.title(win, title)
  tkwm.resizable(win, 0,0)
  #tkconfigure(win, width=width, height=height)

  # commands
  okCommand <- function() if(!tclvalue(bookmVar)=="") tkdestroy(win) else tkfocus(te)
  cancelCommand <- function () {
    tclvalue(bookmVar) <- ""
    tkdestroy(win)
  }
  tkwm.protocol(win, "WM_DELETE_WINDOW", cancelCommand)

  # pack 
  f <- tk2frame(win)
  w <- tk2label(f, text=label, justify="right")
  tkpack(w, side="left", padx=5)
  bookmVar <- tclVar("")
  te <- tk2entry(f, textvariable=bookmVar, width=40)
  tkpack(te, side="left", padx=5, fill="x", expand=1)
  tkpack(f, pady=5)
  f <- tk2frame(win)
  w <- tk2button(f, text=okButLabel, command=okCommand)
  tkpack(w, side="left", padx=5)
  w <- tk2button(f, text="Cancel", command=cancelCommand)
  tkpack(w, side="left", padx=5)
  tkpack(f, pady=5)

  # position
  if(is.null(posx)) posx <- as.integer((as.integer(tkwinfo("screenwidth", win)) - as.integer(tkwinfo("width", win))) / 2.)
  if(is.null(posy)) posy <- as.integer((as.integer(tkwinfo("screenheight", win)) - as.integer(tkwinfo("height", win))) / 2.)
  geom <- sprintf("+%d+%d", posx, posy)
  #print(geom)
  tkwm.geometry(win, geom)

  # run
  tclServiceMode(TRUE)
  ico <- tk2ico.load(file.path(R.home(), "bin", "R.exe"), res = "R")
  tk2ico.set(win, ico)
  tk2ico.destroy(ico)
  tkfocus(te)
  tkbind(win, "<Return>", okCommand)
  tkbind(win, "<Escape>", cancelCommand)
  tkwait.window(win)
  tkfocus(parent)
  return(tclvalue(bookmVar))
}

To plot an heatmap instead of a messagebox, you can use the tkrplot function

library(tkrplot)

heat_example <- function() {
    x  <- as.matrix(mtcars)
    rc <- rainbow(nrow(x), start=0, end=.3)
    cc <- rainbow(ncol(x), start=0, end=.3)
    hv <- heatmap(x, col = cm.colors(256), scale="column", 
        RowSideColors = rc, ColSideColors = cc, margins=c(5,10),
        xlab = "specification variables", ylab= "Car Models",
        main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
}

launchDialog <- function() {  
    ReturnVal <- tkinput(parent=ttMain, title="First Gene", label="Enter A Gene Name")  
    if (ReturnVal == "") return()  

    hmwin <- tktoplevel(ttMain)
    img <- tkrplot(hmwin, heat_example)
    tkpack(img, hmwin)
}   

ttMain <- tktoplevel()  
tktitle(ttMain) <- "ttMain"  
launchDlg.button <- tkbutton(ttMain, text = "Launch Dialog", command = launchDialog)  
tkpack(launchDlg.button, ttMain)

This code produces a heatmap, but gives also an error message I cannot resolve. Maybe someone else here can find the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.