ManuelHentschel / VSCode-R-Debugger

R Debugger Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=RDebugger.r-debugger
MIT License
170 stars 11 forks source link

Support data viewer while debugging #123

Closed gvelasq closed 3 years ago

gvelasq commented 3 years ago

The January 2021 release of the Python extension for VS Code supports a data viewer while debugging. Could this data viewer be ported for use with R objects while debugging? Since it is designed to work with Python objects, it may be compatible with {reticulate} Python objects.

Related: https://github.com/Ikuyadeu/vscode-R/issues/506

renkun-ken commented 3 years ago

If the debug session source vscode-R ~/.vscode-R/init.R then in the debug console, View(obj) could open the data viewer too.

ManuelHentschel commented 3 years ago

Running the following code

Sys.setenv(RSTUDIO="")
Sys.setenv(TERM_PROGRAM="vscode")

source("MY_PATH_TO_VSCODE_R\\vscode-R\\R\\init.R") # <-- replace with path to your init.R

my_function <- function(){
    df <- cars
    print('inside function') # <-- breakpoint here. Then run `View(df)` in debug console.
}

my_function()

with debug config

{
  "type": "R-Debugger",
  "name": "Debug R-File",
  "request": "launch",
  "debugMode": "file",
  "workingDirectory": "${workspaceFolder}",
  "file": "${file}"
}

Produces a similar result as using the data viewer in python. Is this roughly what you had in mind? (Without the manual setting of environment variables and manually running init.R of course).

Introducing a context menu command that opens this data viewer from within a debug session should be doable.

Python's data viewer looks quite nice. Porting it to work with R might be a way to improve the data viewer without too much work for ourselves, but that should probably be considered as part of https://github.com/Ikuyadeu/vscode-R/issues/506.

gvelasq commented 3 years ago

Thank you both. I was able to get the current data viewer to work using View() in the debug console, both with your code @ManuelHentschel and when using the Attach to R process configuration.

I agree that a context menu item would be helpful, as would a View icon on hover in the Run:Variables pane, similar to the one that is visible in the R:Workspace pane.

Bigger picture, porting over the Python extension's data viewer would be an excellent addition.

eitsupi commented 3 years ago

When I followed the above steps in Remote-Containers, I got the following error and the data viewer did not open.

Error in .External2(C_dataviewer, x, title) : unable to start data viewer
In addition: Warning message:
In utils::View(val, node$name) : unable to open display

If I run View(mtcars) after executing .vsc.attach() in the integrated terminal, the viewer will be displayed normally.

image

renkun-ken commented 3 years ago

@eitsupi If you ssh to any remote server and start an R session without sourcing the session watcher init.R and View() any data frame, the same error will occur because there is no display device to show the default viewer. If you enable X11 in the ssh session, then the default viewer should be able to show up then.

eitsupi commented 3 years ago

@renkun-ken Thanks for the reply. I've since figured out on my own that it's probably related to #146, but I don't know what I need to add to the .Rprofile to make it work properly.