I am making a GUI in R using gWidgets. Until now I have been passing values from one window to another via the global environment. Using the global environment is simple to implement but not ideal. One problem is that R CMD check
complains about lacking visible bindings for global variables.
As a solution to this problem, reference classes have been mentioned by several R programmers. But to understand how reference classes would work in this context, it would really help to have a simple example.
Let me give a silly GUI to work with. When the user hits the button of the first window, it puts the model m
in the global environment. The second button gets m
from the global environment and gives an output. When you hit the first button again, it will make a new model m
and change the output of the second button. If you close the first window, the button in the second window will still work, because m
is in the global environment.
library(gWidgets)
options(guiToolkit = "tcltk")
h1 <- function(h, ...){
d1 <- data.frame(x=runif(10), y=runif(10))
.GlobalEnv$m <- lm(x ~ y, data=d1)
}
g1 <- gbutton("1. Make model",
container=gwindow(), handler=h1)
h2 <- function(h, ...){
d2 <- data.frame(y=(1:10)/10)
p <- predict(.GlobalEnv$m, newdata=d2)
print(p)
}
g2 <- gbutton("2. Make prediction",
container=gwindow(), handler=h2)
How can I use reference classes in this example?