使用tcltk输出热图

时间:2012-08-06 20:29:27

标签: r indexing dataframe heatmap

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)

我想重写代码的最后一行,让消息返回热图。我有一个数据框,其中包含所有必需的数据(数据形式的基因表达数据),称为pedM,ReturnVal表示该数据帧中的列名(特定基因)。请帮忙。

任何可以提供的提示都会令人惊叹。 提前致谢。

1 个答案:

答案 0 :(得分:0)

这是一个可能有帮助的例子。您的代码使用AFAIK不存在的modalDialog函数。以下是如何滚动自己的

的示例
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))
}

要绘制热图而不是消息框,可以使用tkrplot函数

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)

此代码生成热图,但也提供了无法解决的错误消息。也许这里的其他人可以找到问题。