clauder1 clauder1 - 23 days ago 8
R Question

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

`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 remove quotation marks from 'file1' (the input from the user in the gedit obj2) to be subsequently used as a PDB file in obj3. e.g. file1 could be "1a3h", and I want it to be edited to: 1a3h.
I've used gsub, but am still got getting the correct output. Where am I going 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(...){
gsub("''", '', file1);
gbutton( "Go", container = innergroup,
handler = function( h, ... ) {
gmessage( svalue( obj2 ), title = (pdb<- read.pdb(file1)))
} )
})

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