VSCode-R-Debugger
VSCode-R-Debugger copied to clipboard
Debug in R markdown file
Is it possible to debug for a code chunk within a r markdown file? I think python jupyter notebook does support the feature, same as r studio.
Unfortunately, this is not possible at the moment. Implementing this properly is going to be difficult (e.g. due to all the different settings/commands that can be specified for each code chunk).
To still allow some Rmd debugging, I can think of the following two alternatives:
- Start a debug session with an empty workspace and send the code inside a chunk to the debug console (e.g. using
ctrl+enter
or the buttons provided by the vscode-R extension). To an extent this might already work. - Convert the entire Rmd file to R using e.g.
knitr::purl()
and debug that. This would ignore the .Rmd specific settings and division into chunks, but allow you to run the R code itself in the debugger. It might also be possible to implement this, so that the conversion is done automatically in the background.
Start a debug session with an empty workspace and send the code inside a chunk to the debug console (e.g. using ctrl+enter or the buttons provided by the vscode-R extension). To an extent this might already work.
That looks reasonable, and I remember the jupyter notebook do the same thing. Convert the whole document to r will lose the advantage of rmd. Though I think the second option may be easier.
I wonder if you could consider a chunk just a piece of code to be executed in the global environment. What variables are defined at debugging time is up to the user. He/she could run any chunks above (that would create those global variables) before debugging whenever needed.
Debugging a chunk is a very important feature in my opinion. Jetbrains' R plug in can do so - RStudio not.
But anyway, VS code's R features incl. this debugger are already excellent! Please go on!
I agree and for what it's worth, where I find using the browser()
debugger useful in rstudio .rmd files is to debug external libraries/other R files, stepping through the function calls to see where an error/bug is coming from. So I wouldn't need/want anything very crazy (or even cross rmd cell boundaries) -- just pulling out the current cell into an execution environment along with all the global/local vars.
If I made the plunge to use VScode over Rstudio I could definitely use purl
ing to get it to work but I wanted to be careful about not losing the current environment.
Start a debug session with an empty workspace and send the code inside a chunk to the debug console (e.g. using ctrl+enter or the buttons provided by the vscode-R extension). To an extent this might already work.
That looks reasonable, and I remember the jupyter notebook do the same thing. Convert the whole document to r will lose the advantage of rmd. Though I think the second option may be easier.
purl()
exported .R
file will have your code chunk separated by ## -----------------
,
and you just simple replace ## -----------------
by # %%
, and you can use the command palette run current chunk
, which makes a R
file runs blockwisely like rmd