VSCode-R-Debugger icon indicating copy to clipboard operation
VSCode-R-Debugger copied to clipboard

Best practise to debug `shiny`

Open markschat opened this issue 4 years ago • 2 comments

I just switched to VSCode for writing Shiny-Apps with custom JS. Loving the experience so far 🤩 thank´s for this great package btw as it adds ton of value! An issue I stumbled upon is that I can´t find any ressources on how to debug shiny using the VSCode-R-Debugger. My own attempts failed.

My question is:

  • Can shiny generally be debugged?
  • If so: what is the best way to do it?

markschat avatar Apr 13 '22 16:04 markschat

Could´nt give up on the idea of using VSCode as my main IDE for R (dark mode, git integration, JS, ... 🥰).

Here is a solution I came up with that seem to work.

Prerequisites

  1. Install/ configure VSCode extensions R (by Yuki Ueda) and R Debugger
  2. Use default shiny app structure (see ?shiny::runApp() for possible setups) Example:
myProject/
├── app.R
├── R/
│   ├── module.R
  1. Set VSCode workspace to the R projects root (e.g. File -> Open Folder... -> myProject)
  2. Create a debug configuration file (prompted for if opening debug tab in VSCode)

Debugging

  • Set your breakpoints using browser() (see https://shiny.rstudio.com/articles/debugging.html)
  • Run Debugger in Launch R-Workspace mode
  • Run shiny::runApp() in the debug console
  • Your app should fire up in a browser and the breakpoints pause the execution 🎉

markschat avatar Apr 14 '22 14:04 markschat

I've found that shiny::runApp() / shiny::shinyApp() will both only actually run the app when print() is called, so changing your script to

app <- shiny::runApp()
print(app)

will probably be what you want to use (with a Launch R-File mode profile)

tomjemmett avatar Jun 06 '22 14:06 tomjemmett