【问题标题】:Display message and rerun the code using gWidgets in R在 R 中使用 gWidgets 显示消息并重新运行代码
【发布时间】:2016-09-07 00:45:04
【问题描述】:
x <- gconfirm("Run Program?",title="gConfirm")

if (x){
    w <- gwindow(title="List of Programs",visible=TRUE)
    g = ggroup(horizontal = FALSE, cont=w)   
    glabel("Please select the Program", cont=g)
    ptype <- c("A","B")
    temp <- gcombobox(ptype , cont=g)
    addHandlerChanged(temp , handler=function(...){})
    gbutton("Run", cont=g,handler = function(...){
    print(svalue(temp)
    dispose(g)
    runagain <- gconfirm("Run again?",title="gConfirm")
    if(runagain){
     ## If user clicks okay, I want to start running again from the third line of the code, w <- gwindow......)**
     }
}

任何人都可以暗示解决这个问题吗?另外,我如何将 svalue(temp) 显示到类似的 UI 框中,而不是在控制台上打印它。非常感谢任何帮助。

【问题讨论】:

  • 首先,要显示 svalue(temp) 的 UI 框,您可以使用 gmessage(svalue(temp))。至于您关于重新运行代码的其他问题,您的意图是什么?是用另一个运行按钮打开一个全新的窗口吗?
  • 为什么每次都要创建/删除/创建组?控制是否发生变化?如果没有,只需使您的运行处理程序以temp 中选择的值为条件。
  • @jav 感谢您的回复。是的,我想再次运行同样的事情,只是 x 的值总是 True,所以我不想使用第一行的 gconfirm。
  • @jverzani 我希望再次能够从程序列表中选择一个程序,所以我需要从一开始就运行它,除了再次询问第一行“运行程序”。

标签: r user-interface gwidgets


【解决方案1】:

这样的事情可能会得到你想要的:

library(gWidgets2)

programs = list("Program A"="a.R",
    "Program B" = "b.R")

w <- gwindow("run programs")
g <- ggroup(cont=w, horizontal=FALSE)
fl <- gformlayout(cont=g)
cb <- gcombobox(names(programs), cont=fl, label="Select a program to run")
b <- gbutton("Run selected program", cont=fl, label="", handler=function(h,...) {
    prog <- svalue(cb)
    val <- gconfirm(sprintf("Run program %s?", prog), parent=w)
    if (val) {
        source(programs[[val]])
        gmessage("All done", parent=w)
    }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多