How to debug R-Shiny code with VS Code - r

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.

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

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")
HTH

Related

Is it possible to run an R markdown script within VS Code without debugging?

I've just started using R in VS Code and my script is currently stored as an RMD file. I'm pretty sure I can't debug this as there's no RMD debugger for the version of R I'm using, but when I try to run without debugging (from Run > Run without Debugging / Ctrl + F5) it prompts a dialogue box saying 'You don't have an extension for debugging R Markdown.' Surely I don't need a debugger to run without debugging?
I can obviously still run chunks at a time, or by selecting code, so this is more of an annoyance than a serious issue.

Shortcut to toggle between R console and the Editor within Intellij/Pycharm

I am using the R console in Intellij. This is started automatically when invoking Run or Debug from within the Editor
From within the R console we can return to the Editor by hitting ESC. How can we then go back to the R console from the Editor via a shortcut (and without hitting Run or Debug again)?
Note: CMD-6 does open the R Tool
But that is not the R console instead some kind of messages window:
Update Here are the Run menu options
Problems and R Console tool windows have the same shortcuts, changing the shortcuts for the tool windows should help:

How to run a shiny app in IntelliJ (PyCharm)

I have a basic shiny app (ui.r and server.r) that I am able to run in RStudio.
But seems like I can't run it in IntelliJ.
I have installed the R language support plug-in and I can run r scripts.
But when I try to run ui.r, the only thing I get is html output in the console.
Does anyone recognise this? I probably forgot to configure something?
I do have experience running java (web) apps with IntelliJ. With tomcat setup e.g.
In the run configurations, I didn't see anything specific for a shiny server or something R related. So I guess that is not where I should search ...
Any idea? Is more information needed?
Thx in adavance.
edit:
The answer is here: https://youtrack.jetbrains.com/issue/R-895
To run a shiny App (R framework) on PyCharm you must enable R first and indicate where is the R interpreter located:
R options appears on the bar status. One of them is R Console. Click on it. Write getwd() command to see if you are in the root directory of the project. If not, set it with setwd() command:
Next, write runApp() and DO NOT press enter yet. You must press alt+enter and then click on load Shiny:

Script using GPU throws error when running app on Shiny server

I have written some code in R using the Shiny package. The app runs alright when running shiny::runApp() from RStudio.
I've tried to host the app on a Shiny server and the GUI starts up correctly. However, a plot should appear on the right when the button "Enviar informacion" is clicked. (You can see how the app looks here, but it won't work though because that link is not on Shiny server but on Shinyapps.io).
The relevant part of the logfile is the following:
Listening on http://127.0.0.1:38327
Loading required package: Rcpp
Warning: Error in [: subscript out of bounds
211: FUN [/srv/shiny-server/spike_sorting/server.R#82]
210: apply
209: cluster_som_h [/srv/shiny-server/spike_sorting/server.R#82]
...
So the error is inside the function cluster_som_h. After trying some stuff, I found out what is causing the error, but don't know how to fix it. Inside this function, I make use of the function Rsomoclu.train() from the package Rsomoclu, with kernelType = 1. This means that the function is run on the GPU. If I set kernelType = 0, then the app works okay with both runApp() and from the Shiny server. So that's where the problem is: for some reason, running the function on the GPU is not working if the app is run from the Shiny server, but it does work if it is run from RStudio using runApp().
Maybe there is something going on because I have CUDA installed on my computer but when I try to run it from the server something happens. Is there a way to fix this? I believe running the code from this specific computer no matter where on the LAN the app is run would solve this. Is this possible?
After doing some (basic) research, I found out what was happening. I have CUDA installed only in my user in Ubuntu, so I had to change the first line of the file /etc/shiny-server/shiny-server.conf so that the user was the right one:
# Instruct Shiny Server to run applications as the user "my_name"
run_as my_name;

Shiny (Rstudio) apps not working

I am working on the Shiny tutorial for RStudio. I updated my RStudio, as indicated in the tutorial. The current version I have is 0.98.945. When I run the supplied examples like runExample("01_hello"), it opens my web browser and shows all the HTML content, but does not seem to be processing the R code.
When I proceed further in the tutorial for creating my own app runApp("App-1"), I get an error message on the web page: ERROR: could not find function "fluidPage". After some sleuthing, I found a posting on another site indicating that I need to download the developer version of shiny from GitHub, found here. I ran the code snippet for that installation -- and still no luck. Any suggestions would be greatly appreciated!
After updating Rstudio to the newest version redo the install.packages("shiny") and library("shiny"). Once you do this it should eliminate the need to manually do the runApp code and a button should appear were the run button usually is that says run app. before you can click run app though you have to set your working directory to the location were you have saved your server.R and ui.R by going to session -> set working directory -> choose directory. chose the folder location then click run app.
Edit: there should be no need for you to run that code from GitHub
I had the same problem. After installing shiny, I had to restart Rstuido for the "Run App" button to appear.
I had also some wired troubles when starting using shiny. Finally I got all fixed by removing all my previous libraries and installations and re-install only the latest versions

Resources