0

I’m new to R world and I’m having some difficulties working with gWidgets and I hope someone out there can help me. First of all my R version is 2.15.2., and I’m using Windows 7 32-Bit.

I want to create a GUI with Input/Output and a selection (Yes, No), so if the user selects (using gradio) “Yes”, will display an extra set of parameters, if “No”, then it should disappear (those parameters), or be gray out. And finally, if the user click OK button, then it will pass some parameters that will be use (later on) to call another function. Here are my questions:

  1. Do you have an idea how can I remove the parameters when the user selects “No”, right now, if I click no, it prints what I want, but when I click “Yes” again, it displays another three parameters.
  2. Do you know how can I pass the arguments when user click “OK”, this is for later when click OK, it will call (or source) another function in a different r code

Many thanks in advance for all your help

Cesar

P.S. Below is my code:

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

#options(expressions=500000)
w <- gwindow("")
g <- ggroup(horizontal = FALSE, container = w)

glabel("Input/Output", container = g)
inputFileDir <- gfilebrowse (text = "Select file...", type = "open", quote = FALSE,
             filter = list("Text File" = list(patterns = c("*.txt"))), container = g)
svalue(inputFileDir)

outputFileDir <- gfilebrowse (text = "Input file name...", type = "save", quote = FALSE,
                             filter = list("Text File" = list(patterns = c("*.txt"))), container = g)
svalue(outputFileDir)

glabel("Direction?", container = g)
DirSelec <- c("Yes","No")
rbF <- function(h,...){
  if (svalue(h$obj, index=TRUE) == 1){
    print ( "define handler here" )

    glabel("Meridional (Raster file):", container = g)
    fieldConstrainDir_v <- gedit("", container = g, default = 0)
    svalue(fieldConstrainDir_v)

    glabel("Zonal (Raster file):", container = g)
    fieldConstrainDir_u <- gedit("", container = g, default = 0)
    svalue(fieldConstrainDir_u)

    glabel("Max. Angle:", container = g)
    maxAng <- gedit("", width = 3, initial.msg = "Paste the path to the raster file (no extensions)", default = 0, container = g)
    svalue(maxAng)
  }else {
    #(svalue(h$obj, index=TRUE) == 2)
    print ( "When User click NO, it needs to go or gray out" )
  }
}

rb <- gradio(DirSelec, container = g)
selected = svalue(rb, index=TRUE) <- 2
rbH <- addHandlerClicked(rb, handler = rbF)

bg <- ggroup(container = g)
addSpring(bg)
onOK <- function(h,...){
  print(svalue(inputFileDir))
  #chartr("\\", "/", print(svalue(inputFileDir)))
  print(svalue(outputFileDir))
  print(svalue(fieldConstrainDir_v))
  print(svalue(fieldConstrainDir_u))
  print(svalue(maxAng))

}

gbutton("    OK    ", container = bg, handler = onOK)
gbutton(" Cancel ", container=bg, handler = function(h,...) dispose(w))
Cesar
  • 3
  • 2

2 Answers2

3

You have various options:

  • you can place the extra parameters into a gexpandgroup widget and call the visible<- method on this. This will toggle showing them.

  • Within gWidgetsRGtk2, you can create the widgets in a (child) container that is not attached to a parent container (no cont=... in the constructor), then add(parent, child) to add and delete(parent, child) to remove. This shouldn't delete the widgets in the rm sense, just remove them from the screen

  • You can place the extra parameters into a container and call enabled<- with FALSE to "gray" them out.

The first two may require some screen size management.

In all the cases, the child controls will still be settable and readable during the program, so you should have your default values there or do some checking when you use them.

As for passing parameters a good way within R is to put the control widgets into a list, say l. Then this idiom

out <- sapply(l, svalue)

bundles them up into a list that can be passed to your function. The do.call function makes it easy to work with lists as arguments.

jverzani
  • 5,600
  • 2
  • 21
  • 17
  • many thanks John (@jverzani), I'm going to modify them based on your comments, one thing though, when I tried to set the default values (i.e., fieldConstrainDir_v <- gedit("", container = g, default = 0)) is not passing the values, am I doing something wrong? – Cesar Feb 18 '13 at 23:30
  • You might want `gedit("0", container=g, coerce.with=as.numeric)`. There is no `default` argument. (The coerce.with function is called on the string before svalue.) – jverzani Feb 19 '13 at 01:46
  • Hi John (@jverzani), thanks for your help, I modified the code based on your comments and I'm able to pass the parameters in a list, I change it to lapply as I need individual parameters. However, I'm still confused and not able to extract and assign a variable name based on my selection in a gcombobox. To be more clear, if I select "Dis", then I need load the script that has the function “Dis.r”, with the appropriate parameters, if I select "IDis", then will load and run (with the appropriate parameters names) the “IDis.r” code. This is an example of the code: – Cesar Feb 20 '13 at 22:56
  • Well, you want to use one value to pick the file. Say you use sapply(l, svalue, SIMPLIFY=FALSE) so you get a list with named components. Then some function like function(bname, ...) = {with(list(...), source(sprint("%s".R, bname),local=TRUE)} where bname is the variable name of the combobox (and a component of the list from sapply) – jverzani Feb 21 '13 at 14:37
0

Here is the code:

require(gWidgets)
w <- gwindow("") 
g <- ggroup(cont = w, horizontal = FALSE) 
g1 <- ggroup(cont = w) 
Vl <- list () 

fr3 <- gframe ("", cont=g, horizontal=FALSE) 
l3 <- glayout ( cont = fr3 , expand=TRUE) 
l3 [1,1] <- NbS <- glabel("Type", cont = l3) 
l3 [1,2] <- (Vl$NbS <- gcombobox (c("Dis","IDis","K"), cont = l3)) 

fr4 <- gframe ("", cont=g, horizontal=FALSE) 
l4 <- glayout (cont=fr4, expand = TRUE) 
l4 [1,1] <- Dm <- glabel("Dist", cont = l4) 
l4 [1,2] <- (Vl$Dm <- gedit("0", cont = l4))

rbC <- function (h,...){ 
  out <- lapply(Vl, svalue) 
  print(out) 
  if (out$NbS == "Dis") {
    print("Dis") 
    print(out$Dm) 
    # Dm <- get(out$Dm, get(svalue(Dm))) 
    # Dm <- get(out$Dm) 
    # dsrc <- source(".../Dis.r") 
    # print (do.call (dsrc, out)) 
    } else if (out$NbSelec == "IDis") {
      print("IDis") 
      }
  } 
ok <- gbutton("OK", cont = g1, handler=rbC) 

When I print, is showing me the variables name and values, what I want is to able to use the variables (which have the same name as in my "Dis.r") and with do.call run the r script depending on my selection.

Thanks a lot C

Cesar
  • 3
  • 2