Search code examples
ruser-interfacerstudiogeditgwidgets

How do you give functionality to a gWidget and how to utilise the input by a user into 'gedit'?


`win <- gwindow(title = "Analysing PDB structures", visible=TRUE, name=title,
           width = NULL, height = NULL, parent=NULL)
group <- ggroup(horizontal = FALSE, container=win)
obj <- glabel("Type your PDB code here:", container = group)
obj <- gedit("", container=group)
obj <- gbutton("Go", container = group)`

When the user inputs a value to the gedit and presses the gbutton "Go", how can you get the subsequent code (e.g. install.packages(bio3d)) to automatically run?

EDIT: In response to the post I've managed to work the functionality, thank you. How can I utilise the input given by the user in a 'gedit' in obj2 in obj3? What am I doing wrong?

win <- gwindow(title = "Analysing PDB structures", 
           visible=TRUE, name=title,
           width = NULL, height = NULL, parent=NULL)
group <- ggroup(horizontal = FALSE, container=win)
obj1 <- glabel("Type your PDB code here:", container = group)
innergroup <- ggroup(container = group)
obj2 <- gedit((file1<-""), container=innergroup)
obj3<-addHandlerChanged(obj2, handler=function(...){
  gbutton( "Go", container = innergroup, 
       handler = function( h, ... ) {
         gmessage( svalue( obj2 ), title = (pdb<- read.pdb(file1)))
       } )
 })

Solution

  • Just add a handler:

    win <- gwindow( title = "Analysing PDB structures", 
                    visible=TRUE, name=title,
                    width = NULL, height = NULL, parent=NULL)
    group <- ggroup( horizontal = FALSE, container=win )
    obj1 <- glabel( "Type your PDB code here:", container = group )
    obj2 <- gedit( "", container = group )
    obj3 <- gbutton( "Go", container = group, 
                    handler = function( h, ... ) {
        gmessage( svalue( obj2 ), title = "" )
    } )
    

    Put your code instead the gmessage(), or define your function before and just refer to it by name: handler = foo. That function must be defined following this pattern:

    foo <- function( h, ... )
    { 
        gmessage( svalue( obj2 ), title = "" )
    }