Question

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))
Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top