clauder1 clauder1 - 23 days ago 6
R Question

How do you give functionality to a gWidget and remove quotation marks around an input?

`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. I have now realised a new issue, how can I remove quotation marks around the input, given by the user into the gedit (obj2), to be used in obj3? I've named it 'file' here; it saves the input with the quotation marks, is there a way around this or do I have to manually remove the "" from the input?

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

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