library(RGtk) library(gtkDevice) # This function is the same as the first layout function except that now we # have a GUI that does something when the button is clicked. # The function sstartSim is called when the button is clicked. layouts2 = function(){ # These variables can be accessed by the startSim function because they are # in the parent environment of startSim selectedDist = "Poisson" sampleSize = 2 selectedStat = "Mean" # The w argument will contain the widget object when called # We don't need to do anything with this object startSim <- function(w) { N = 1000 * sampleSize samples = rpois(N, 1) samples = matrix(samples, nrow = 1000) sampleStats = apply(samples, 1, mean) plot(density(sampleStats), main="Sampling Distribution of Mean for 2 Observations from the Poisson") } win = gtkWindow() # The v box is a vertical box to contain widgets # We need to add the vbox to the window vbox = gtkVBox() win$Add(vbox) # Create a graphics device to put in the GUI histPlot <- gtkDrawingArea() asGtkDevice(histPlot) vbox$PackStart(histPlot) # This is the button that will start the simulation # We have added a call back to the button, that specifies the function # to call when the event "clicked" occurs. goBtn = gtkButton("Start Simulation") goBtn$AddCallback("clicked", startSim) vbox$PackEnd(goBtn, FALSE, FALSE) }