vscode-R icon indicating copy to clipboard operation
vscode-R copied to clipboard

Using jupyter/ipython as R terminal

Open david-cortes opened this issue 2 years ago • 11 comments

When using the python extension, one can launch an interactive jupyter interpreter, select an R kernel there, and execute R commands interactively, but trying to use it as Rterm doesn't work - for example, if I have the following file set as Rterm, it fails to start it:

#!/usr/bin/env sh
jupyter console --kernel=ir

Would be ideal if one could use that as the R terminal in this extension, since it's a more handy environment - e.g. it allows clicking some piece of text and moving the edit focus there, which the plain terminal and radian don't do.

david-cortes avatar Aug 09 '22 19:08 david-cortes

As a workaround, you can start it directly in the terminal. That is, create a new terminal and run jupyter console --kernel=ir. Also enable the setting R: Always Use Active Terminal. Then any command that sends text to the console will send it to the current terminal, which is now running the Jupyter console.

andycraig avatar Aug 11 '22 02:08 andycraig

@andycraig But it doesn't send inputs to it when it is launched like that. Additionally, it wouldn't look the same way as when using the Python extension but writing R code with an R kernel.

Here's a video showing more or less how it works under the python extension: r_kernel

david-cortes avatar Aug 11 '22 18:08 david-cortes

But it doesn't send inputs to it when it is launched like that.

It should work, so if this is something you're interested in could you provide more details about what happens when you try it? E.g., what command you are using to send inputs, and where the inputs go instead.

Additionally, it wouldn't look the same way as when using the Python extension but writing R code with an R kernel.

That's true - what I suggested was different from the Jupyter Interactive Window. Thank you for the video. Using the Jupyter Interactive Window with vscode-R's features that send text to the console isn't possible at the moment. But this functionality could be added. The Jupyter extension provides a command jupyter.execSelectionInteractive() that sends text to the Interactive Window's console. We could add a feature that allows the R extension to call that function instead of sending text to the VS Code terminal.

One option for doing this would be to add a new setting, like R: Always Use Jupyter Interactive Window. When the setting is enabled, all commands that currently send text to the VS Code terminal would instead send text to the Jupyter Interactive Window console. (How this setting interacts with R: Always Use Active Terminal would need to be considering.)

It might need a volunteer to implement this setting - would you be interested in trying?

CC @DonJayamanne who has discussed Jupyter interop with us previously.

andycraig avatar Aug 13 '22 00:08 andycraig

If I set the sh script mentioned at the beginning as Rterm (adding "$@" at the end just in case), then I get this error when it comes the time to execute some R command in the terminal:

david@debian:~$ jupyter qtconsole --kernel=ir
libGL error: MESA-LOADER: failed to open radeonsi: /usr/lib/dri/radeonsi_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
libGL error: failed to load driver: radeonsi
libGL error: MESA-LOADER: failed to open radeonsi: /usr/lib/dri/radeonsi_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
libGL error: failed to load driver: radeonsi
libGL error: MESA-LOADER: failed to open swrast: /usr/lib/dri/swrast_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
libGL error: failed to load driver: swrast

If I launch the command in the vscode terminal and select R: Attach active terminal, then nothing happens - next time I try to execute some R command, it launches a new terminal.

If I launch a jupyter interactive interpreter, set an R kernel, select some text in an R file, press Ctrl+Shift+P, and type jupyter, I don't get any option containing anything similar to "execute".

I'll have to pass on implementing the seeting for Always Use Jupyter Interactive Window as I'm not familiar with vscode extensioning.

david-cortes avatar Aug 14 '22 13:08 david-cortes

If I launch the command in the vscode terminal and select R: Attach active terminal, then nothing happens - next time I try to execute some R command, it launches a new terminal.

Can you check whether the setting R: Always Use Active Terminal is enabled (set to true)? This sounds like what would happen if it was not enabled.

andycraig avatar Aug 14 '22 13:08 andycraig

If I launch the command in the vscode terminal and select R: Attach active terminal, then nothing happens - next time I try to execute some R command, it launches a new terminal.

Can you check whether the setting R: Always Use Active Terminal is enabled (set to true)? This sounds like what would happen if it was not enabled.

Thanks, it works after I tick that setting, although it doesn't work with jupyter qtconsole (which allows clicking on text to switch focus).

david-cortes avatar Aug 14 '22 13:08 david-cortes

when using the interactive window in Jupyter extension, the code is sent directly to the selected R juoyter kernel. if this is to be changed so that the text is sent elsewhere (like the R terminal), then id suggest creating a separate interactive window for R (something that will be owned by vscode-r), that way we can control the the behavior instead of special casting the jupyter extension to make a special case for R or the like.

DonJayamanne avatar Aug 15 '22 05:08 DonJayamanne

@david-cortes Glad to hear that it works for jupyter console now. It looks like jupyter qtconsole might be a bit more complex - it may well be that getting the Interactive Window from the Jupyter extension to work would be easier than getting the Qt Console to work.

andycraig avatar Aug 15 '22 08:08 andycraig

Hi @DonJayamanne, thanks for your notes.

when using the interactive window in Jupyter extension, the code is sent directly to the selected R juoyter kernel.

This is how I'm imagining it. At the moment vscode-R basically just pastes text into the terminal. But we could modify vscode-R so that there's the option to send text directly to the R Jupyter kernel.

if this is to be changed so that the text is sent elsewhere (like the R terminal)

Fortunately I don't think this would be necessary. It would be either/or: Either the user chooses to send the text to the R terminal (which we currently do), or the user chooses to send the code directly to the R Jupyter kernel.

I think the Jupyter extension already provides everything needed. The only modification I think is needed is to vscode-R, so that it can call the Jupyter extension command to send text to the R Jupyter kernel.

andycraig avatar Aug 15 '22 09:08 andycraig

Here's the function that sends text to be executed. What I'm imagining is a modification that checks whether the user has set R: Always Use Jupyter Interactive Window and if so calls jupyter.execSelectionInteractive() instead of term.sendText().

https://github.com/REditorSupport/vscode-R/blob/da579cc17a9485f7a11bf5e75bf6b9c347eca116/src/rTerminal.ts#L230

andycraig avatar Aug 15 '22 10:08 andycraig

There was a mention in VScode 1.86 release notes that now it should be easier for extensions to run code through jupyter.

The notes pointed to the following repository with examples: https://github.com/microsoft/vscode-extension-samples/tree/main/jupyter-kernel-execution-sample

Is this what would be needed to use the jupyter interactive window as R terminal?

david-cortes avatar Feb 01 '24 17:02 david-cortes