Thursday, 15 March 2012

user interface - Loading and saving variables in R with gWidgets -



user interface - Loading and saving variables in R with gWidgets -

for past few months i've been building simulation in r hope package. consists of 2 usable functions, , many internal ones 1 of 2 usable functions phone call while looping, perform stages of simulation.

a simple conceptual illustration is:

# abstract representation 1st usable function, generates object containing settings simulation. settings <- function(){ matrix(c(1:4),nrow=2,ncol=2) } # abstract representation of 1 of many internal functions, action in simulation. int.func <- function(x){ out <- x*2 return(out) } # abstract representation of sec usable function, takes settings & invokes internal functions generates results & saves r object files. sim.func <- function(x){ ans <- int.func(x) ans2 <- ans-2 save(ans2, file="outtest") }

with bundle far, using done after loading , attaching bundle library():

input <- settings() fix(settings) # if want alter defaults. sim.func(input)

nothing needs returning simulation function because results , data-dumps saved object save() commands , objects can read in afterwards.

now i'd have simple gui can used in conjunction command line - think rcmdr much more simple, allow co-workes have never touched r utilize it. gui needs able edit settings - prepare command above, save settings object file, , read in setting object file. i've built gwidgets:

gui <- function(){ input <- matrix(c(1:4),nrow=2,ncol=2) mainwin <- gwindow("mainwindow") button1 <- gbutton("edit settings", cont=mainwin, handler= function(h,...){ fix(input) print("settings edited") }) button2 <- gbutton("run", cont=mainwin, handler= function(h,...){ sim.func(input) print("the run done") }) savebutton <- gbutton("write settings file",cont=mainwin, handler= function(h,...){ setfilename <- ginput("please come in filename") save(input, file=setfilename) }) loadutton <- gbutton("load settings file", cont=mainwin, handler=function(h,...){ fname <- gfile(test="choose file", type="open", action="print", handler = function(h,...){ do.call(h$action, list(h$file)) } ) load(fname)}) }

note job of settings function before done on first line of gui function. add together same r file 3 functions above, add together gui namespace export, set gwidgets , gwidgetstcltk imports, , rebuild, library() bundle , gui().

the interface shows up. have few issues: gui shows fine, if click button1 ("edit settings") edit settings through fix(input), alter values, close editor , click button 1 time again see if changes have persisted , been stored in input, have not. same goes reading in object, not overwrite input object generated default in first line of function gui().

i think has environments of functions i'm not sure. in gui-less version of package, user generates object containing settings, in workspace , feeds simulation function argument. since gui version, run within function gui() , gwidgets handlers makes utilize of functions(h,...) can't help sense if environments issue here. it's odd when clicking on button 1, find input gui() environment, won't create changes there.

can help out , suggest need do?

apologies long question, i've tried explain clearly. code reproducible, issue, having library(gwidgets, gwidgetstcltk) , copying , pasting code i've provided here, define functions , running gui(). click "edit settings" button, alter cells, exit, click button 1 time again see if changes persisted (they don't). abstract illustration i've provided faithfully reproduces same issues have proper simulation functions if can't working, won't real thing working.

thanks,

ben w.

uea

the sainsbury laboratory.

[edit] here fix/workaround using .globalenv:

gui <- function(){ input <- matrix(c(1:4),nrow=2,ncol=2) .globalenv$input <- input mainwin <- gwindow("mainwindow") button1 <- gbutton("set", cont=mainwin, handler= function(h,...){ input <- .globalenv$input fix(input) print("settings have been edited...") }) button2 <- gbutton("run", cont=mainwin, handler= function(h,...){ sim.func(.globalenv$input) print("the run done") }) writebutton <- gbutton("write settings file",cont=mainwin, handler= function(h,...){ setfilename <- ginput("please come in filename") input <- .globalenv$input save(input, file=setfilename) }) loadutton <- gbutton("load settings file", cont=mainwin, handler=function(h,...){ fname <- gfile(test="choose file", type="open", action="print", handler = function(h,...){ do.call(h$action, list(h$file)) } ) load(fname) .globalenv$input <- input}) }

r user-interface load save gwidgets

No comments:

Post a Comment