gWidgets: How to save user's work in the application?

86 Views Asked by At

I have been building application in R with gWidgets. Application includes several editable tables, working sheets etc - basically works something like MS Excell, but for special purpose.

When the user will save the work done and exit application, he/she should be able to continue working from where he/she ended - something like clicking on the icon or importing "working document" into the application - so that all sheets and data will be opened (again something like MS Excell etc.) and the same as they were when application was closed.

However, I have no idea how to do that. Can anyone help?

1

There are 1 best solutions below

1
jverzani On BEST ANSWER

Some pattern like this should work:

library(gWidgets2)

# Global variables
widgets <- list()
values <- NULL # or a list()
state <- NULL

## Layout GUI
w <- gwindow("Test")
g <- ggroup(cont=w)
g1 <- ggroup(horizontal=FALSE, cont=g)
save_btn <- gbutton("Save state", cont=g1)
restore_btn <- gbutton("restore state", cont=g1)

widgets$w1 = gedit("some text", cont=g)
nb = gnotebook(cont=g)
widgets$w2 = gradio(c("a","b","c"), label="radio", cont=nb)
widgets$w3 = gtable(mtcars[1:3,1:3], label="table", cont=nb)

# Save and restore actions
addHandlerClicked(save_btn, handler=function(...) {
    values <<- lapply(widgets, svalue, index=TRUE)
    state <<- lapply(widgets, "[")
})

addHandlerClicked(restore_btn, handler=function(...) {
    if (!is.null(values)) {
        for (nm in names(widgets)){
            ## screen for widgets without values
            if (length(state[[nm]]))
                widgets[[nm]] <- state[[nm]]
            print(list(widgets[[nm]], values[[nm]]))
            svalue(widgets[[nm]], index=TRUE) <- values[[nm]]
        }
    }
})