Best practise to debug `shiny`
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?
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
- Install/ configure VSCode extensions R (by Yuki Ueda) and R Debugger
- Use default shiny app structure (see
?shiny::runApp()for possible setups) Example:
myProject/
├── app.R
├── R/
│ ├── module.R
- Set VSCode workspace to the R projects root (e.g. File -> Open Folder... -> myProject)
- 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 🎉
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)