vscode-R
vscode-R copied to clipboard
Using jupyter/ipython as R terminal
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.
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 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:
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.
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.
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.
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 totrue
)? 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).
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.
@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.
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.
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
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?