Skip to content
Ceres Barros edited this page Nov 7, 2017 · 16 revisions

Debugging with SpaDES

Eliot J. B. McIntire October 14, 2016

Debugging in R and SpaDES can work in several ways. These include the core of R and RStudio tools, as well as several options that are SpaDES specific.

Built-in R and RStudio default traceback and error inspector.

Simulation debugging using browser()

Insert a browser() call anywhere in your module, which allows you to step through your module code. See ?browser for more on this. This allows the user to enter directly into the module code as it is running. After inserting a browser() call, remember to save the R script and rerun the simInit( ) call so that R knows about the browser() call that has been added.

Simulation debugging using the debug argument

The spades function takes an argument, debug. See ?spades. Currently, this can take several values, specifically, logical, character, list of logical or characters.

This approach is useful during spades() calls but (obviously) not during simInit().

debug = TRUE

This will output to the console some current event details as it is executing. This is relatively fast and has little impact on simulation speed. This can be helpful when trying to identify where in the bug is located in the simulaion event chain.

spades(mySim, debug = TRUE)

debug = functionName

If a function is passed to debug, then it will be applied to the simList. So, any simList accessor, such as 'time', 'objects' etc. will be output as each event is executed.

spades(mySim, debug = 'objects')

debug = "simList"

This will output the entire simList as each event is executed.

debug = "character string with a ( somewhere (indicating it includes a function call)"

This will be evaluated by the R interpreter, inside a context where there access to the simList, using the name sim.

spades(mySim, debug = "paste0('Time: ', time(sim), ', Number of Events so far: ', NROW(completed(sim)))")

debug = moduleName

If your module contains the line:

if (debug) {print(paste0('Event type is: ', eventType)); browser()} 

as the first line in your doEvent.moduleName function definition, then setting the debug argument to the module name will cause a break in the code inside the doEvent function. This can be very effective for seeing how the doEvent file is being executed. With clever use of the interactive tools in Rstudio in the debugger mode, or just using the key shortcuts (see ?browser), the debugger can go into subsequent functions.

spades(mySim, debug = "wolfAlps")

# Then use keyboard shortcuts:
#  n for next line
#  s for step into a function
#  Enter for repeat previous keyboard shortcut