I am using Quarto and VSCode to try to run Python and R together. I'd like to be able to access R objects in python or python objects in R. If I render the document below, it works as expected. However, if I am running in interactive mode, i.e., running each chunk / developing the code, I cannot access objects from the other language. It seems they do not share the same environment. Is this possible or am I misunderstanding the abilities of Quarto and reticulate?
---
title: "Untitled"
format: html
---
```{r}
library(reticulate)
reticulate::use_virtualenv("/Users/xxx/.pyenv/versions/xxx")
```
```{r}
x <- 123
```
```{python}
y = r.x * 2
print(f"Python y = {y}")
```
```{r}
# Access Python variable from R
cat("R accessing Python y:", py$y, "\n")
# Create an R data frame
df_r <- data.frame(
a = 1:3,
b = c("x", "y", "z")
)
print(df_r)
```
```{python}
# Access R data frame from Python
import pandas as pd
df_python = r.df_r
print("Python accessing R data frame:")
print(df_python)
print(f"Type: {type(df_python)}")
```
For reference, I am running Quarto in VS Code.
if (!py_available()) {stop("Python not available")}; py_run_string("print('Python ready')"). Maybe explicitly initializing the python session would help. I can dig a bit deeper when I am back at my desk tmrw.vscode://settings/quarto.cells.useReticulateenabled?