Saurabh Palatkar
Saurabh Palatkar

Reputation: 3384

How to debug R-Shiny code with VS Code

Is there any way to debug Shiny (R) inside VS Code? I can easily debug Shiny apps inside RStudio but I like to code inside VS Code so looking for the way to debug R code inside VS Code.

Upvotes: 8

Views: 3838

Answers (2)

Daniel M Bader
Daniel M Bader

Reputation: 115

I was not able to see plots or html (e.g. from plotly) when in debugging mode. I already had "httpgd" as plotting device enabled. The option says it also changes the corresponding R option vsc.use_httpgd, but I checked it was NULL. Setting it explicitly in my .Rprofile or also live in the debugging session allowed me to see plots again, e.g. via print(plot(1:5)).

# For your .Rprofile
options("vsc.use_httpgd" = TRUE)

# code to check if option is set
getOption("vsc.use_httpgd")

Upvotes: 0

Mac Jo
Mac Jo

Reputation: 51

Seems, shiny can be debugged in VSCode using browser-statements 🙌

Prerequisites

  • Setup this VSCode extensions (VSCode R Guide)
    • R (by Yuki Ueda)
    • R Debugger
  • Use a default app structure (see docs of appDir when running ?shiny::runApp for possible setups)
  • Set your VSCode workspace to the R projects root (e.g. File -> Open Folder... -> myProject)

Debugging

  • Set your breakpoints using browser() (RStudio Article)
  • Run R-Debugger in Launch R-Workspace mode
  • Execute shiny::runApp() in the Debug Console as soon the Debugger pauses
  • Your app should fire up in a browser and the breakpoints pause the execution 🎉

Further Ressources

  • VSCode Article on debugging (can be very helpfull)
  • Issue on this topic in the VSCode-R-Debugger repo

Upvotes: 3

Related Questions