0

Below is an artificial example of a R widget

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

f <- function(file, max.rows){
    dat <- read.table(file, nrows=max.rows)
return(max(dat[,]))
}

lst <- list() 
    lst$action <- list(beginning="f(",ending=")")
    lst$arguments$file <- list(type="gfilebrowse")
    lst$arguments$max.rows <- list(type="gedit", text=-1)

ggenericwidget(lst, container=gwindow("Example"))

widget

The name of each argument in the R widget is the same as its name in the R function. Is it possible to only change the name appearing in the R widget ? For instance, I would like to write "Maximum number of rows" in the R widget instead of "max.rows".

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225

3 Answers3

1

All I could come up with was the following, working from this post. I'm not sure if you're willing to create the widgets from scratch, but it seems to me that it's the only way to manually choose labels for your arguments.

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

f <- function(file, max.rows){
   dat <- read.table(file, nrows=max.rows)
   return(max(dat[,]))
}

win <- gwindow("Example")

grp.text <- ggroup(horizontal=FALSE, container = win)
lbl.text <- glabel("Maximum Lines: ", container = grp.text)
insert.text <- gedit(-1, container = grp.text)

grp.file <- ggroup(horizontal=FALSE, container = win)
lbl.file <- glabel("File: ", container = grp.file)
browse.file <- gfilebrowse(container = grp.file)

read <- gbutton(text="Go", container = grp.file, 
    handler = function(h, ...) {
            cat(f(svalue(browse.file), 
                as.numeric(svalue(insert.text))));
    }
)

This is a minimal example; there would have to be some error-checking along the way.

Edward
  • 5,367
  • 1
  • 20
  • 17
  • That seems to work but I don't like the output: with my widget the R code "f(file=..., max.rows=...) " appears in the console, not with yours. I will think about your way and try to get something closest to what I want. Thanks. – Stéphane Laurent Jul 24 '12 at 16:50
  • What Edward shows is the best solution -- just make your own widgets. the `ggenericwidget` function is pretty limited. In `gWidgets2` I dropped this function. The `gformlayout` container makes such things fairly easy. Here is an example https://github.com/jverzani/gWidgets2/blob/master/inst/examples/ex-gformlayout.R. As well, the main functionality of `ggenericwidget` is contained in this example https://github.com/jverzani/gWidgets2/blob/master/inst/examples/ex-generate-gui.R – jverzani Jul 24 '12 at 19:21
  • @jverzani Thanks a lot. I think it is not a good idea to drop the ggenericwidget() function. Please listen to my story. I am working in a statisticians team in which I am the only real R user (my colleagues are SAS users). I gave a little presentation about gWdigtes. I said to my colleagues: "hey! take a R function, use ggenericwidget and look, the widget is done !". They were impressed and charmed. Then I add: "this is the basic way, it's possible to do a more sophisticated and pretty widget". They wouldn't have been such enthusiastic if I had shown them a code such yours or Edward's code... – Stéphane Laurent Jul 25 '12 at 07:01
1

To flesh out my comment to Edward's answer, this is how gformlayout from gWidgets2 can be used, though using gtable directly might also be the way to go.

library(gWidgets2)
options("guiToolkit"="RGtk2")

f <- function(file, max.rows){
    dat <- read.table(file, nrows=max.rows)
    message("Calling max")
    print(max(dat[,]))
}

## containers
w <- gwindow("Example", visible=FALSE)
fr <- gframe("Arguments", horizontal=FALSE, cont=w)       # optional frame
fl <- gformlayout(cont=fr)
## widgets
select_file <- gfilebrowse(cont=fl, label="File")
max_rows <- gedit(-1, cont=fl, label="maximum number of rows", coerce.with=as.numeric)
## button
bg <- ggroup(cont=fr)
addSpring(bg)
btn <- gbutton("ok", cont=bg)

addHandlerClicked(btn, function(h,...) {
  l <- svalue(fl) ## a named list by the labels,
  do.call(f, setNames(l, c("file", "max.rows"))) ## change names for do.call
})
visible(w) <- TRUE

I haven't put gWidgets2 on CRAN yet (still on github), but will at some point. This is where any new features will be made for gWidgets going forward.

jverzani
  • 5,600
  • 2
  • 21
  • 17
-1

The alternative to the big solution from @user1546302 uses back ticks: (Note that there were several unmatched brackets in your original post. I tried to correct this, but SO refused because it were not enough characters changes).

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

f <- function(file, max.rows){
  dat <- read.table(file, nrows=max.rows)
  max(dat[,])
}

lst <- list() 
lst$action <- list(beginning="f(",ending=")")
lst$arguments$file <- list(type="gfilebrowse")
lst$arguments$`max rows` <- list(type="gedit",text=-1)

ggenericwidget(lst, container=gwindow("Example"))
Dieter Menne
  • 10,076
  • 44
  • 67