1  Introduction

We often write functions or modularise our codes to integrate a complex protocol with multiple steps. In this process, we use the outputs from one function as inputs to another function and execute the entire pipeline sequentially. While working in a team with multiple levels of contributions, it is unlikely that the complete pipeline will work seamlessly at the first instance.

In general, two types of errors may occur. The first is the syntactic error, where the script stops immediately due to the incorrect syntax. Although the error message may be sometime cryptic, the syntactic error is generally easy to spot, taking appropriate measures to rectify it. On the other hand, the semantic or logical error is difficult to capture as the code will run with or without a warning message and always without any error message. The outcomes from a function with semantic errors do not provide the correct or intended results. It requires meticulous investigation of each operation in a function.

For programming languages like R, convenient options like functions within a function, lexical scoping, lazy evaluation, and three dots (ellipsis) enable additional efficiency, but sometimes create unintended operations making it harder to spot semantic errors. Although the error in a script is frustrating, it is also a delightful experience to do detective work with a lens and identify the offending bugs! It is vital for any serious programming project: to collaborate in a team to debug and improve scripts, explore the generalisability of scripts to wider data sources and conduct reproducible research.

This workshop will demonstrate the approach to debugging R script, primarily focussing on semantic errors. Using bite-sized examples, we will first discuss the fundamental building block of the R function: function arguments, global and local environments, and other essential function components. In the second section, we will investigate key R debugging tools like traceback, debug and browser and venture into the detective work to identify the bugs. The third section will extend the techniques to debug base R functions and functions in pre-installed R packages. In the final section, we will illustrate the application of similar principles in debugging Python scripts.

Participants are expected to be familiar with either R or Python. The principles of debugging and associated tools are similar in both programming environments; however, the workshop will mainly focus on R, with examples provided for both R and Python. All instructions to install necessary programs and tutorial materials will be available online before the session. Participants can follow and run scripts on their laptops during the workshop session.