Debug R Packages

Specify debug messages as special string constants, and control debugging of packages via environment variables.


Linux Build Status Windows Build status CRAN RStudio mirror downloads Coverage Status

Specify debug messages as special string constants, and control debugging of packages via environment variables. This package was largely influenced by the debug npm package.

Installation and Usage

Install the package from GitHub:

source("https://install-github.me/r-lib/debugme")

To use debugme in your package, import it, and then add the following .onLoad function to your package:

.onLoad <- function(libname, pkgname) {
  debugme::debugme()
}

You can now add debug messages via character literals. No function calls are necessary. For example:

"!DEBUG Start up phantomjs"
private$start_phantomjs(phantom_debug_level)
 
"!DEBUG Start up shiny"
private$start_shiny(path)
 
"!DEBUG create new phantomjs session"
private$web <- session$new(port = private$phantom_port)
 
"!DEBUG navigate to Shiny app"
private$web$go(private$get_shiny_url())

The string literals are simply ignored when debugging is turned off. To turn on debugging for a package, set the environment variable DEBUGME to the package name you want to debug. E.g. from a bash shell:

export DEBUGME=mypackage

Or from within R:

Sys.setenv(DEBUGME = "mypackage")

Separate multiple package names with commas:

export DEBUGME=mypackage,otherpackage

The debug messages will be prefixed by the package names, and assuming your terminal supports color, will be colored differently for each package.

Example

Dynamic code

The debugme debug strings may contain R code between backticks. This code is evaluated at runtime, if debugging is turned on. A single debug string may contain multiple backticked code chunks:

"!DEBUG x = `x`, y = `y`"
if (x != y) {
...

Motivation

I have always wanted a debugging tool that

  • is very simple to use,
  • can be controlled via environment variables, without changing anything it the packages themselves,
  • has zero impact on performance when debugging is off.

debugme is such a tool.

Performance

Function calls are relatively cheap in R, but they still do have an impact. If you never want to worry about the log messages making your code slower, you will like debugme. debugme debug strings have practically no performance penalty when debugging is off.

Here is a simple comparison between debugging with a function call, f1(), debugging with debug strings, f2() and no debugging at all.

debug <- function(msg) { cat(msg, file = "/dev/null", "\n") }
f1 <- function() {
  for (i in 1:100) {
    debug("foobar")
    Sys.sleep(.001)
  }
}
f2 <- function() {
  for (i in 1:100) {
    "!DEBUG foobar"
    Sys.sleep(.001)
  }
}
f3 <- function() {
  for (i in 1:100) {
    Sys.sleep(.001)
  }
}
microbenchmark::microbenchmark(f1(), f2(), f3(), times = 10L)
#> Unit: milliseconds
#>  expr      min       lq     mean   median       uq      max neval
#>  f1() 173.6354 175.8429 178.7392 178.5770 181.7964 184.2407    10
#>  f2() 140.3894 141.3177 143.6294 143.4185 145.5860 148.1551    10
#>  f3() 139.4864 141.3185 143.1088 142.5824 144.5418 146.5900    10

License

MIT © Gábor Csárdi

News

1.1.0

  • Support functions in lists and environments. In particular, this fixes debugging R6 methods (#15)

  • Support DEBUGME_OUTPUT_DIR (#19)

  • Support log levels (#12)

  • Fix functions without arguments (#17)

  • Print the debug stack, optionally (@kforner, #21)

1.0.2

  • Do not us testthat::with_mock, it interferes with the JIT that is default in R 3.4.0. Use the mockery package instead.

1.0.1

  • Fix a test case bug.

1.0.0

First public release.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("debugme")

1.1.0 by Gábor Csárdi, 4 months ago


https://github.com/r-lib/debugme#readme


Report a bug at https://github.com/r-lib/debugme/issues


Browse source code at https://github.com/cran/debugme


Authors: Gábor Csárdi


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports crayon, grDevices

Suggests covr, mockery, R6, testthat, withr


Imported by callr, cranlike, processx, webdriver.

Suggested by batchtools.


See at CRAN