Chapter 9 Tips & Tricks

9.1 Debugging

If you’ve historically used R interactively, you may find it difficult to define functions that get executed at once without your input as Plumber requires. There are a couple of debugging techniques to be aware of when working on your Plumber APIs; these techniques are equally transferrable to debugging your R scripts, packages, or reports.

9.1.2 Interactive Debugging

Print debugging is an obvious starting point, but most developers eventually wish for something more powerful. In R, this capacity is built in to the browser() function. If you’re unfamiliar, browser() pauses the execution of some function and gives you an interactive session in which you can inspect the current value of internal variables or even proceed through your function one statement at a time.

You can leverage browser() when developing your APIs locally by adding a browser() call in one of your filters or endpoints and then visiting your API in a client. This offers a powerful technique to use when you want to inspect multiple different variables or interact with the current state of things inside of your function. This is also a good way to get your hands dirty with Plumber and get better acquainted with how things behave at a low level. Consider the following API endpoint:

#' @get /
function(req, res){
  browser()
  
  list(a=123)
}

If you run this API locally and then visit the API in a web browser, you’ll see your R session switch into debug mode when the request arrives, allowing you to look at the objects contained inside your req and res objects.

9.2 Testing

// TODO docs for testing

9.3 Organizing Large Applications

// TODO docs for organizing large apps

9.4 Swagger

// TODO Docs for Swagger

9.5 Performance

//TODO Run some load tests and show the performance/overhead of running Plumber.