how best to debug dialog and run code

27 views
Skip to first unread message

Vincent

unread,
Nov 13, 2012, 4:46:47 AM11/13/12
to ded...@googlegroups.com
It took me a longgggg time to figure out a reasonably efficient way to test and debug my code for a menu using deducer. In case this is useful to others I am pasting a simple example below. If you source this code from the JGR console (with Deducer loaded) you get the dialog to pop-up and can test if the code is working as expected. If not, make changes to the code and source again, and again, and again, as needed. If there are other, perhaps more effective, ways to debug I'd luv to hear about them. 

.olsCheckFunction <- function(state) {
if(length(state$dep) < 1)
return("Please select a dependent variable")
if(length(state$indep) < 1)
return("Please select at least one independent variable")
return("")
}

.olsRunFunction <- function(state) {

form <-paste( " ~ " , state$indep[1])
for(var in state$indep[-1])
form <- paste(form,"+",var)

cmd <- paste("summary(ols <-lm(", state$dep, form, ", data =", state$data, "))")

execute(cmd)
}

makeOLSDialog <-function() {

# make the dialog
dialog <- new(SimpleRDialog)
dialog$setSize(500L, 400L)
dialog$setTitle("Regression")

# add a variable selector
variableSelector <-new(VariableSelectorWidget)
variableSelector$setTitle("data")
variableList <- new(VariableListWidget, variableSelector)
addComponent(dialog, variableSelector,1,450,850,10)

# add the dependent variable
dep <- new(SingleVariableWidget, "Dependent Variable", variableSelector)
dep$setTitle("dep")
addComponent(dialog, dep, 50 ,1000, 250, 500)

# add the independent variables
indep <- new(VariableListWidget, "Independent Variables", variableSelector)
indep$setTitle("indep")
addComponent(dialog, indep, 300,1000, 850, 500)

# call the check and run functions
dialog$setCheckFunction(toJava(.olsCheckFunction))
dialog$setRunFunction(toJava(.olsRunFunction))

# For debugging, comment out the following line. Use dialog$run() instead.
### return(dialog)
dialog$run()

}

# simulate data
x1 <- rnorm(100)
x2 <- rnorm(100)
y <- 4 + 2*x1 - .4*x2 + rnorm(100,sd = .2)
df <- data.frame(cbind(y,x1,x2))

# debug the run-function or the dialog
### runfunc <- TRUE
runfunc <- FALSE
if(runfunc == TRUE) {
.olsRunFunction(list(
'data'="df",
'dep'=c("y"),
'indep'=c("x1","x2")
))
} else {
makeOLSDialog()
}

Vincent

unread,
Nov 13, 2012, 5:11:43 PM11/13/12
to ded...@googlegroups.com
I guess the title of my post might be a bit misleading. I am not suggesting my way 'is' the best way to debug your deducer code :) I should have added a '?', i.e., 'how best to debug dialog and run code?'

Ian Fellows

unread,
Nov 18, 2012, 3:57:27 PM11/18/12
to ded...@googlegroups.com
That is pretty much correct. It should be said that programming in
general has a large component of
bang-head-on-table-why-won't-this-work to it, especially when you are
starting out. One thing that is useful is to print out things with the
cat function.

Debugging dialogs in R is actually quite a bit easier than in Java, as
there is no need to recompile or reinstall a package.

best,
Ian
Reply all
Reply to author
Forward
0 new messages