quarto-dev / quarto-nvim

Quarto mode for Neovim
Other
330 stars 12 forks source link

Help needed to run reticulate #115

Closed andy941 closed 5 months ago

andy941 commented 5 months ago

Hi,

After a bit of fiddling around I was able to set up Quarto in my neovim config with Molten support. It's great, finally a way to do data science from inside neovim. The last thing I am missing is passing python objects to R, something that will be very valuable in my job. That would be the dream.

I can run python and R code blocks no problem and knitr can produce the expected document when passing an object (string) to R, but when I run the code cells manually (in molten you have two kernels active, one for python and one for R), the same object is returned NULL by reticulate.

example:

format: html title: "TRIAL" author: "Andrea Finocchio"

bibliography: cite.bib

citeproc: true useMath: true standalone: true html: embed-resources: true self-contained-math: true code-fold: true engine: knitr

library(reticulate)
# Define the cars vector with 5 values
r_array <- c(1, 3, 6, 4, 9)
import sys
print(sys.path)
to_send = "TRY"
print(to_send)
dat <- py$to_send
print(dat)
reticulate::py_config()

knitr output: image

last cell output when run with Quarto+Molten: image

MoltenInfo: image

Is that functionality implemented? Is there a specific setup I should follow? I would appreciate help I just started getting into Quarto and I have never really used jupyter. I used to use RStudio and the Nvim-R for my data science needs, just plain R scripts (awful I know) and I am now mainly a C++ developer.

Thank you for your time, Andrea

jmbuhr commented 5 months ago

I don't think jupyter kernels can talk to each other like that, so molten can't to that (correct we if I'm wrong, @benlubas).

If you do the same in RStudio, you notice that what RStudio actually does is open the reticulate python console in your current R sesssion (you'll see the prompt changing, but it's still inside oft the R console). We can replicate the same by sending code to neovim's integrated terminal in which we have an R session with vim-slime (example config https://github.com/jmbuhr/quarto-nvim-kickstarter/blob/4bb2c1f30d5282720b9cde39c33537b59fd33ada/lua/plugins/quarto.lua#L635) and then opening the python session in there with https://rstudio.github.io/reticulate/reference/repl_python.html

This could be done automatically, but I haven't implemented this so far. The pieces are there, as I'm already tracking if the cursor is in a python chunk to change the way vim-slime sends code to be compatible with ipython here: https://github.com/jmbuhr/quarto-nvim-kickstarter/blob/4bb2c1f30d5282720b9cde39c33537b59fd33ada/lua/plugins/quarto.lua#L638-L653

jmbuhr commented 5 months ago

ok, I couldn't stop myself from building this, as I thought about that being useful anyways...

https://github.com/jmbuhr/quarto-nvim-kickstarter now automatically opens and closes the reticulate python repl in your R session if you opened an R terminal through the <leader>cr keybinding (here is the commit that makes it work https://github.com/jmbuhr/quarto-nvim-kickstarter/commit/23c5536b0106d4d3cd000bb475c8ba7acf38f9f6 in case you want to roll your own).

image

jmbuhr commented 5 months ago

note, that the python language server has no idea that this is happening, so it will complain about e.g. not knowing the r object.

andy941 commented 5 months ago

That looks great, maybe that could be possible with molten too one day. I have had problems with setting up vim-slime and I don't understand exactly how it works but I'll take a stab at it again when I have time.

benlubas commented 5 months ago

@andy941 have you tried using molten with just an r kernel and running code in the same order @jmbuhr did in that example?

It looks like R is handling all of this and the rticulate package is running its own ipython kernel or a modified one or something.

This is a total shot in the dark. I've never used R before.

jmbuhr commented 5 months ago

I think it's not that straightforward, because jupyter is very much cell-based, so if you attempt to open a different repl in a cell it halts execution because it is waiting for input. As such, the next cell is never executed and I don't think there is a notion of sending input to a waiting cell. image

That's why both RStudio and the Quarto VS Code extension also send python code within an R session to an R console and in general don't use the jupyter R kernel. e.g. https://github.com/quarto-dev/quarto/commit/5b1e799aae821798d28e24e0dddca0fa31544884

benlubas commented 5 months ago

Oh yeah.

In that case I don't think there's much we can do with molten. Unless you want to copy paste your code into vim.ui.prompt lol.

andy941 commented 5 months ago

ok, I couldn't stop myself from building this, as I thought about that being useful anyways...

https://github.com/jmbuhr/quarto-nvim-kickstarter now automatically opens and closes the reticulate python repl in your R session if you opened an R terminal through the <leader>cr keybinding (here is the commit that makes it work jmbuhr/quarto-nvim-kickstarter@23c5536 in case you want to roll your own).

image

It does not work for me, I fixed it like this:

-- This does not work
local is_python = require("otter.tools.functions").is_otter_language_context("python")

-- This works (gets first word)
local is_python = require("otter.keeper").get_current_language_context():gmatch("%w+")() == "python"

is_otter_language_context uses get_current_language_context to get the language in the cell. problem is that functiondoes not return only the language name but also information about the chunk length or something-> i.e.: python 20 1 30 2 My solution just simply grab the first word. Could you check and fix it? It works for me beautifully now.

P.S: this is AWESOME.

andy941 commented 5 months ago

As a side note, should BASH cells work too? It's really not important, just out of curiosity.

benlubas commented 5 months ago

I have bash cells configured to run with vim-slime. But you can also send something like this: !pwd to the ipykernel and it will run pwd (or whatever command you put after the !).

or you can use the bash cell magic to turn an entire cell into bash commands, and send that to the ipython kernel.

jmbuhr commented 5 months ago

Apologies, the is_otter_language_context actually works now. I had accidentally moved the commit to a different branch when I fixed other things...

I tend to have a separate (integrated) terminal open, e.g. one for ipython and one for bash, and manually switch for quick interactive stuff (https://github.com/jmbuhr/quarto-nvim-kickstarter/blob/e718c76068a13d7d5db704a346eea2d8b6778367/lua/plugins/quarto.lua#L700-L702), but one could automate this in a similar manner.