gWidgets包装

我为R模拟做了一个GUI,并将其绑定到packages目录的R文件夹中它自己的.R文件中的一个函数中:

gui <- function(){

  mainwin <- gwindow("speEaR - Simulation of Plant-pathogen Effectors in Evolutionary Arms Races")

  introtext <- glabel("speEaR - Simulation of Plant-pathogen Effectors in Evolutionary Arms Races, version 1.1 This package is maintained by Ben ward. <b.ward@uea.ac.uk>", cont = mainwin, markup=TRUE)

  moretext <- glabel("To begin, edit the settings of the simulation. Settings are stored in a matrix and can be read 
                     in from .R object files.
                     For help and documentation click the help button, or use R's documentation system.", cont=mainwin)

  main_grp <- ggroup(container = mainwin)

  settings_grp <- ggroup(container = main_grp, horizontal=FALSE)

  settings_frame <- gframe("Settings", cont=settings_grp)

  editsetbutton <- gbutton("Edit Settings", cont=settings_frame,
                           handler=function(h,...){
                             fix(SETTINGS)
                           })

  savesetbutton <- gbutton("Write Settings to File", cont=settings_frame,
                           handler=function(h,...){
                             settingsfilename <- ginput("Please enter the filename to save this settings matrix to. Bear in mind this will save to the current working directory of R.",text="", title="Filename?", icon="question")
                             save(SETTINGS, file=settingsfilename)
                           })



  loadsetbutton <- gbutton("Load Settings", cont=settings_frame,
                           handler=function(h,...){
                             fname <- gfile(test="Choose a file", 
                                            type="open", 
                                            action="print",
                                            handler = 
                                              function(h,...){
                                                do.call(h$action, list(h$file))
                                              }
                             )
                             load(fname)})

  runhelp_grp <- ggroup(cont=main_grp)

  runsimbutton <- gbutton("Run Simulation!", cont=runhelp_grp, expand=TRUE,
                          handler=function(h,...){
                            run.Sim(SETTINGS)
                          })

  helpbutton <- gbutton("Help", cont=runhelp_grp,
                        handler=function(h,...){
                          visible(helpwin) <- TRUE
                        })

  addSpring(runhelp_grp)

  helpwin <- gwindow("Help Pages", visible=FALSE)

  helpWidget <- ghelp(container = helpwin, expand=TRUE)

  add(helpWidget, list(topic="Values.gen", package="speEaR"))

  add(helpWidget, list(topic="Mutate", package="speEaR"))

  add(helpWidget, list(topic="Evolve.Probs", package="speEaR"))

  add(helpWidget, list(topic="Change.Expression", package="speEaR"))

  add(helpWidget, list(topic="Dup.Dels", package="speEaR"))

  add(helpWidget, list(topic="run.Sim", package="speEaR"))

  add(helpWidget, list(topic="makeSetMatrix", package="speEaR"))

  add(helpWidget, list(topic="speEaR", package="speEaR"))

}

我知道它可以通过运行gWidgets和gWidgetstcltk的全新R会话来运行。

我已将gWidgets和gWidgetstcltk作为导入包含在NAMESPACE文件中。 但是,如果我构建并重新加载包(我在Rstudio中),并键入控制台gui()

它告诉我Error in gui() : could not find function "gwindow" gwindow是gui()函数使窗口包含所有内容的第一件事情。

我还测试了我没有搞砸我的模拟包装,做了一个新的包装,只包含一个简单的功能,只用一个按钮加载一个窗口:

gui <- function(){
  boop <- gwindow("HI WORLD")
  button <- gbutton("WOOP!", cont=boop)
}

然后我在描述文件中指定了Imports: gWidgets, gWidgetstcltk ,我构建并重新加载。 我犯了同样的错误。

有没有人经历过这个? 当这两个依赖包含在NAMESPACE的导入下时,为什么会在构建的包中发生这种情况? 构建和重新加载软件包不会显示任何错误。 关键在于将GUI与包装配合使用,因此可以在包装加载时调用gui函数,从而使用户更容易。

谢谢,Ben W. UEA TSL

[编辑]为了回应我现在所做的一个建议:

gui <- function(){
  boop <- gwindow("HI WORLD")
  button <- gbutton("WOOP!", cont=boop)
}

.onLoad <- function(libname, pkgname){
  gui()
}

但是我不熟悉使用.onLoad以及我是否正确地完成了这一操作。 我已经阅读了文档,并且试图了解R如何在加载包和命名空间,钩子等方面的工作,但是如果行话太频繁,我会努力工作,所以我仍然认真思考。 但是,构建包并重新加载给了我这个新错误:

Error : .onLoad failed in loadNamespace() for 'testgui', details:
  call: function (classes, fdef, mtable) 
  error: unable to find an inherited method for function ‘.gwindow’ for signature ‘"NULL"’
Error: loading failed

编辑2:

我一直在做更多的事情,我想我有一个可行的抽象例子:

# An abstract example of package function to generate simulation settings:
settings <- function(){
  matrix(c(1:4),nrow=2,ncol=2)
}

# An abstract example of internal function of simulation, which the simulation function
# itself calls many times during its loops:
int.func <- function(x){
  out <- x*2
  return(out) 
}

# Abstract example of main simulation function, which calls internal functions and saves 
# results to R files. 
sim.func <- function(x){
  ans <- int.func(x)
  save(ans, file="outtest")
}

在我当前的包中,这些由控制台调用:

input <- settings() # Generate settings for sim.
fix(input) # Edit settings if desired.
sim.func(input) # Run the sim with the settings defined by variable 'input'

我的贵族如下:

gui <- function(){
  mainwin <- gwindow("MainWindow")
  button1 <- gbutton("Set", cont=mainwin, handler=
                       function(h,...){
                         fix(INPUT)
                         print("addy is set")
                       })
  button2 <- gbutton("RUN", cont=mainwin, handler=
                       function(h,...){
                         sim.func(INPUT)
                         print("The run is done")
                       })
  savebutton <- gbutton("Save Settings",cont=mainwin, handler=
                          function(h,...){
                            setfilename <- ginput("Please enter the filename")
                            save(INPUT, file=setfilename)
                          })

  loadutton <- gbutton("Load Settings", cont=mainwin,
                   handler=function(h,...){
                     fname <- gfile(test="Choose a file", 
                                    type="open", 
                                    action="print",
                                    handler = 
                                      function(h,...){
                                        do.call(h$action, list(h$file))
                                      }
                     )
                     load(fname)})
}

我的名字空间文件是:

export(gui)
import(gWidgets, gWidgetstcltk)

这成功地在R中生成并重新加载,并且当gui()被调用时它就会出现。 现在测试它。

链接地址: http://www.djcxy.com/p/71155.html

上一篇: gWidgets packaging

下一篇: How to find out which package version is loaded in R?