clauder1 clauder1 - 26 days ago 6
R Question

How do you give functionality to a gWidget?

`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: Managed to work the functionality, thanks. Just one last thing: how can I get the input of the gedit (obj2), to be used in obj3 and applied to the title, to be read by the PDB? I've named it 'input' here, but it isn't recognised by the PDB, even when in the correct format?

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((input<-""), container=innergroup)
obj3 <- gbutton( "Go", container = innergroup,
handler = function( h, ... ) {
gmessage( svalue( obj2 ), title = (pdb<- read.pdb(input)) )
} )

Answer

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 = "" )
}