0

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.

2
  • 1
    In your first chunk, add 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. Commented Aug 18 at 6:52
  • Do you have vscode://settings/quarto.cells.useReticulate enabled? Commented Aug 18 at 9:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.