# Here are a few pieces for the CLT GUI that demonstrate some # of the other input widgets, e.g. spin boxes and option menus myCLT = function(stats = c("Mean", "Median", "Min"), dists = c("Normal", "Poisson", "Uniform")) { library(RGtk) library(gtkDevice) # Variables needed for the simulation sampleSize = 100 replicate = 1000 selectedStat = stats[1] # The possible values for stats -- when the option menu changes # this function is called. Notice that it has two arguments, # w will contain the widget and stat will contain one of the Mean, Median, etc # When this function is called, it resets the value of selectedStat in the # parent environment. setStat <- function(stat, w){ selectedStat <<- stat } # selectDist is a similar function # The runsimulation function is called when the Start button is pushed runSimulation <- function(w) { # here we get the value of the spin box only when the start button # is clicked, not when the value is changed in the spin box sampleSize <<- ssSB$GetValueAsInt() N = replicate * sampleSize samples = switch(selectedDist, "Normal" = rnorm(N), "Poisson" = rpois(N, 1), "Uniform" = runif(N)) samples = matrix(samples, nrow = replicate) statistic = get(tolower(selectedStat), mode = "function") sampleStats = apply(samples, 1, statistic) plot(density(sampleStats), main=paste("Sampling Distribution of", selectedStat, "for", sampleSize, "Observations from the", selectedDist, sep=' ')) } win = gtkWindow() vbox = gtkVBox() win$Add(vbox) # Create a graphics device to put in the GUI # Plot to the device # You may need to resize the window to see it # Like in the layout function we create button to start the simulation # The other controls are placed in an hbox # We can nest boxes within boxes to control the layout hbox = gtkHBox() vbox$PackEnd(hbox, FALSE, FALSE) # Here we create a spin button for selecting the sample size # It has two pieces, the adjustment and the button sampSLab = gtkLabel("Sample Size: ") ssADJ <- gtkAdjustment(1,1,400,1,1,1) ssSB <- gtkSpinButton(ssADJ,0,0) ssSB$SetUsize(80,20) hbox$PackStart(sampSLab,expand=FALSE) hbox$PackStart(ssSB,expand=FALSE) # Here we create a choice menu to select the statistic # As with the spin button there are two parts, the option menu # and the information that goes in it, which itself contains # menu items. # Also note that the function call to AddCallBack has an extra argument # This argument contains either "Mean" or "Median" or "Min" -- which is # the value of stat vector statsOMenu <- gtkOptionMenu() statsMenu <- gtkMenu() for (i in 1:length(stats)) { item <- gtkMenuItem(stats[i]) item$AddCallback("activate",setStat,stats[i]) statsMenu$Append(item) } statsOMenu$SetMenu(statsMenu) statLab = gtkLabel("Choose a Statistic: ") hbox$PackStart(statLab,expand=FALSE) hbox$PackStart(statsOMenu,expand=FALSE) # Here we create a set of radio buttons to choose the distr # The unusual thing about radio buttons is that they need to be grouped # in order to limit one button at a time being pushed. # To do this, each subsequent button is linked to the first button # when it is created. distLab<-gtkLabel("Distribution: ") hbox$PackStart(distLab,expand=FALSE) firstButton <- NULL for (i in dists) { distBtn <- gtkRadioButton(firstButton, i) if (is.null(firstButton)) { firstButton <- distBtn selectDist(i) } distBtn$AddCallback("clicked",selectDist, i) hbox$PackStart(distBtn,expand=FALSE) } }