# Debugging R in VSCode

R terminal supports interactive debugging via browser() command.

When browser() is hit, the R terminal enters the debugging mode so that user could evaluate any R expression in the current environment, e.g. inspect the variables (ls.str()), or run commands such as print the calling stack (where), continue excution (c), go to next statement (n), step into function calls (s), etc. If you want to know more, Advanced R: Debugging is quite helpful on this topic.

Many R users, however, might not be familiar with command line interfaces, then it could be difficult to debug in the terminal since it does not show any useful context on each breakpoint and we have to query what we need to see. Usually, we are debugging because we observe something is wrong but we are not sure what is wrong. Debugging is usually a process of guessing what is wrong and validating our assumptions. Therefore, it is not always obvious to know what to see before digging into the problem. And that’s why a command line debugger might not be the best tool for many users to find bugs.

A visual debugger could be more productive in many cases. RStudio has a visual debugger that automatically shows the variables and the call stack on breakpoints. It is much more user-friendly than the command line debugger via browser(). Debugging with RStudio is a comprehensive introduction to the RStudio debugger capabilities.

In the recent months, the VSCode R Debugger and its backend R package vscDebugger have been under active development, thanks to the great efforts from Manuel Hentschel.

The extension implements the Debug Adapter Protocol so that it could work in VSCode with the VSCode Debuger API. The debugger starts a standalone R session (unlike RStudio using the active R session) and allows user to launch a workspace, a file, or a function.

To launch a workspace, for example, user could put the following configuration into .vscode/launch.json in the workspace folder:

{
"type": "R-Debugger",
"request": "launch",
"name": "Launch Workspace",
"debugMode": "workspace",
"workingDirectory": "\${workspaceFolder}",
"allowGlobalDebugging": true
}


Then clicking the Launch button in the debugger pane or hitting F5 will start a new R session. Do it again to source the active script file. Whenever a breakpoint is hit, or an error occurs, the debugger will stop at the event and the variables and call stack are displayed in respective panes in the debugger side view.

When a breakpoint is hit, or an error occurs, user could evaluate any R expression in the debug console and see the output in a more structured way.

The VSCode R Debugger is not yet released to the VSCode Marketplace. To try it, please go to https://github.com/ManuelHentschel/VSCode-R-Debugger/releases and download the extension build and R package from the latest release.

The R debugger extension is still experimental and thus certainly has some known limitations and bugs. If you have any suggestions or find any bugs, please report to VSCode-R-Debugger. Suggestions and contributions are always welcome!