gWidgets packaging

I've made a GUI for an R simulation and bound it to a function in it's own .R file in the R folder of the packages directory:

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"))

}

I know it works by running it through a fresh R session with gWidgets and gWidgetstcltk loaded.

I've included gWidgets and gWidgetstcltk in the NAMESPACE file as imports. However, if I build and reload the package (I'm in Rstudio), and type in the console gui()

It tells me Error in gui() : could not find function "gwindow" gwindow being the first thing the gui() function does to make the window containing everything.

I also tested I hadn't screwed up packaging of my simulation, by making a fresh package containing nothing but the simple function which only loads up a window with a button:

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

I then specified Imports: gWidgets, gWidgetstcltk in the description file, I built and reloaded. I get the same error.

Has anybody else experienced this? Why would this happen in a built package when the two dependencies are included in the NAMESPACE under imports? Building and reloading the package does not show up any errors. The whole point is to have the GUI in with the package so the gui function can be called at package loading, making it easier for the user.

Thanks, Ben W. UEA TSL

[EDIT] In response to a suggestion I've now done:

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

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

However I'm unfamiliar with using .onLoad and whether I have done this correctly. I've read the documentation and I'm trying to learn how R works with loading packages and namespaces, hooks etc but I struggle if jargon gets too frequent so I'm still figuring it out. However, building the package and reloading gives me this new error:

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

EDIT 2:

I've been working things some more and I think I have a working abstract example:

# 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")
}

In my current package these are called by console:

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'

My gui is below:

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)})
}

My namespace file is:

export(gui)
import(gWidgets, gWidgetstcltk)

This successfully builds and reloads in R, and when gui() is called it appears. Now testing it.

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

上一篇: 使用建议包时配置NAMESPACE和说明

下一篇: gWidgets包装