Call setRefClass
, and include each widget and data value as a field. Widgets should have type ANY
. Initialize those widgets in the initialize
method, and outsource functionality to other methods. Create a function to wrap the creation of the class.
silly_gui_generator <- setRefClass(
"SillyGui",
fields = list(
#widgets
win1 = "ANY",
win2 = "ANY",
button1 = "ANY",
button2 = "ANY",
#data
modelData = "data.frame",
predictionData = "data.frame",
model = "lm"
),
methods = list(
initialize = function(modelData = NULL)
{
if(is.null(modelData))
{
modelData <<- data.frame(x = runif(10), y = runif(10))
}
win1 <<- gwindow(visible = FALSE)
win2 <<- gwindow(visible = FALSE)
button1 <<- gbutton(
"1. Make model",
container = win1,
handler = function(h, ...)
{
makeModel()
}
)
button2 <<- gbutton(
"2. Make prediction",
container = win2,
handler = function(h, ...)
{
print(predictModel())
}
)
visible(win1) <- TRUE
visible(win2) <- TRUE
},
makeModel = function()
{
model <<- lm(x ~ y, data = modelData)
},
predictModel = function()
{
predictionData <<- data.frame(y = (1:10) / 10)
predict(model, newdata = predictionData)
}
)
)
generate_silly_gui <- function(modelData = NULL)
{
invisible(silly_gui_generator$new(modelData = modelData))
}