# This is an implementation of the t-test demo # provided by the tcltk package. # This version is a Java implementation and will hopefully # allow one to see the similarities and differences between # the Java and Tcl/Tk interfaces. # See also the document TkJava (in Postscript, PDF, etc.) dialog.t.test <- function() { # This is called when the user clicks on the Submit # or Reset button. # We determine which button was clicked by examining # its action command. Then we call the corresponding # function. Note that these functions can be called directly # without a user clicking on a button. This is a good way to # to organize GUI code so that it is not tied to a particular # design of the graphical interface. actionPerformed <- function(ev) { cmd <- ev$getActionCommand() if(cmd == "Reset") reset() else { print(compute()) } NULL } # Computes which alternative hypothesis is currently selected. # This returns the label of the selected radio button within # that group. getAlternative <- function() { ans = group$getSelection()$getActionCommand() if(is.null(ans)) ans = "two.sided" ans } # Determines whether the "equal variance" checkbox is # currently selected or not. getEqualVariance <- function() { equalVar.btn$isSelected() } # This computes the value of T test, having retrieved # the different parameters from the GUI's current state. compute <- function() { x <- get(vars[[1]]$getText()) y <- get(vars[[2]]$getText()) alt <- getAlternative() vv <- getEqualVariance() e = quote(t.test(x, y, alternative = alt, var.equal = vv)) eval(e) } # Here we export the functions that Java will call # when it receives events that are to be passed to this # "object". We create the object with the appropriate # function corresponding to the Java method expected # in the Java listener. Then we store this in the foreign # reference manager. obj <- list(actionPerformed=actionPerformed) ref <- foreignReference(obj) # Now we can create a Java class and an instance of it # that is an ActionListener and a reference to an R object. jdynamicCompile("java.awt.event.ActionListener", "RButtonListener") l <- .JNew("RButtonListener", ref) # Now we create the graphical interface. For simplicity, we # arrange the components in a 4 x 1 grid. We create a centered label # saying t-test. Then we create the two text fields # in which the user specifies the R variables containing the # data to be used in the t-test. This pair of input fields is # contained in its own panel and occupies the second row of the top-level # grid. # The third row contains a pair of elements: the checkbox for the # equal variance setting and a collection of 3 radio buttons specifying # the alternative hypothesis to use when computing the critical value for # the test. # Finally, we add the pair of buttons: Submit and Reset. panel <- .JNew("JPanel") panel$setLayout(.JNew("GridLayout", as.integer(4), as.integer(1))) panel$add(.JNew("JLabel", "t-test", .Java("SwingConstants","CENTER"))) # Create a panel containing the two textfields # for specifying the variables, and store these for use # in the callback function(s). vars <- list() tmp <- .JNew("JPanel") tmp$setLayout(.JNew("GridLayout", as.integer(2), as.integer(1))) for(i in c("x", "y")) { lab <- .JNew("JLabel", paste(i,"variable")) b <- .JNew("JTextField") tmp1 <- .JNew("JPanel") tmp1$setLayout(.JNew("BorderLayout")) tmp1$add("West", lab) tmp1$add("Center", b) vars[[i]] <- b tmp$add(tmp1) } panel$add(tmp) # The equal variance check box. tmp <- .JNew("JPanel") tmp$setLayout(.JNew("GridLayout", as.integer(1), as.integer(2))) equalVar.btn <- .JNew("JCheckBox", "Equal variance") tmp$add(equalVar.btn) # The alternative hypothesis radio buttons. tmp1 <- .JNew("JPanel") tmp1$setLayout(.JNew("GridLayout",as.integer(3), as.integer(1))) group <- .JNew("ButtonGroup") for(i in c("two.sided","less", "greater")) { b <- .JNew("JRadioButton", i) if(i == "two.sided") twoSided <- b group$add(b) tmp1$add(b) } tmp$add(tmp1) panel$add(tmp) # The 2 buttons - Submit and Reset. tmp <- .JNew("JPanel") tmp$setLayout(.JNew("GridLayout", as.integer(1), as.integer(2), as.integer(10), as.integer(4))) for(i in c("Submit", "Reset")) { b <- .JNew("JButton", i) tmp$add(b) b$addActionListener(l) } panel$add(tmp) .JNew("GenericFrame", panel) # This function is called in response to the user # pressing the Reset button. reset <- function() { for(i in vars) i$setText("") twoSided$setSelected(T) } reset() return(ref) }