r - 如何从 gwidgets 中的函数返回值

标签 r gwidgets

我有以下代码来选择输入和输出文件夹的目录。

require(gWidgets2RGtk2)

input=NULL
win = gwindow("Stage 1")
g = ggroup(horizontal=FALSE,cont=win)
  g1=ggroup(cont=g)
    svalue(input)<-gbutton("Input folder",cont=g1,expand=TRUE,handler=function(...) {
      input=gfile(type="selectdir")
      gmessage(paste0("Input directory set to ",input))
      input
    })
g2=ggroup(cont=g)
    gbutton("Ouput folder",cont=g2,expand=TRUE,handler=function(...) {
      output=gfile(type="selectdir")
      gmessage(paste0("Output directory set to ",output))
    })

但是,在运行时,代码不会返回输入和输出文件夹目录。谁能告诉我我在这里做错了什么?

最佳答案

为了详细说明我的评论,这里有一个同时使用环境和 <<- 的示例。

require(gWidgets2)
options(guiToolkit="RGtk2")

input <- NULL                           # global
e <- new.env()                          # environment

win = gwindow("Stage 1")
g = ggroup(horizontal=FALSE,cont=win)

gbutton("Input folder",cont=g,handler=function(...) {
  value <- gfile(type="selectdir")
  gmessage(paste0("Input directory set to ",value))
  ## assign via <<-
  input <<- value
  ## assign to an environment
  e$input <- value
})

gbutton("What is stored?", cont=g, handler=function(...) {
  print(sprintf("The global variable has %s", input))
  print(sprintf("The environment stores %s", e$input))
})

关于r - 如何从 gwidgets 中的函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21106214/

相关文章:

r - Shiny 中的嵌套响应式(Reactive)表达式

r - 如何在 R 中加载 MASS 库的数据集部分?

r - 按组展开日期

linux - 在 vagrant box 上安装 R-Script 和 RMySQL

r - 如何在 R 中使用 gWidgets 渲染打印?

r - 在 R 中正确设置时间序列的问题

r - 在 gWidgetsRGtk2 中使用 ggplot2

r - 如何从 gWidgets 和处理程序返回值?

r - 为特定命名空间中的对象赋值 - R