tcltk R - 如何访问函数返回的值

时间:2015-05-07 21:41:46

标签: r tk rgui

我刚开始使用tcltkR。当调用第二个函数myFun1时,我正在通过名为myFun2的函数访问计算值时遇到麻烦:

以下是我的用户界面的简化版本:

简单的tcltk接口

library(tcltk)
tt <- tktoplevel()
    topMenu <- tkmenu(tt)
    tkconfigure(tt, menu = topMenu)
    fileMenu <- tkmenu(topMenu, tearoff = FALSE)
        tkadd(fileMenu, "command", label = "Function1", command = myFun1)
        tkadd(fileMenu, "command", label = "Function2", command = myFun2)
        tkadd(topMenu, "cascade", label = "Tab", menu = fileMenu)
tkfocus(tt)

我的功能

myFun1 <- function() { 
    compVal <- 2*3
    compVal
}

myFun2 <- function() { 
    msg <- paste("The value is: \n", compVal )
    mbval<- tkmessageBox(title="This is the title",
                     message=msg,type="yesno",icon="question")
}

调用myFun1有效,但myFun2返回

  

粘贴错误(“值为:\ n”,compVal):     找不到对象'compVal'

compVal包装到return(compVal)中也不起作用。 我也想做:

res <- list(compVal=compVal)

但我无法使用myFun2访问创建的列表。 有关如何在myFun1内找到myFun2的返回值的任何内容吗?

1 个答案:

答案 0 :(得分:0)

I found a solution, at first I thought its not really a "clean" way of doing it, but even in the offical documentation it is done this way. Simply create a global variable using for (var i = 0; i<= Object.keys(response.result).length; i++ ) { if(response.result[0].runners[i].runnerName.indexOf('Su Jeong') >0 { document.body.innerHTML = response.result[0].runners[i].selectionId; } } :

<<-