I try to call an R object from Python inside a Quarto document:
---
title: "pandas"
format: html
jupyter: python3
---
```{r}
data("penguins", package = "palmerpenguins")
```
```{python}
penguins=r.penguins
penguins
```
When I execute the chunks one by one in RStudio, everything is okay:
> data("penguins", package = "palmerpenguins")
> reticulate::repl_python() # automatically executed by RStudio
Python 3.10.4 (/Users/.../3.10.4/bin/python3.10)
Reticulate 1.24 REPL -- A Python interpreter in R.
Enter 'exit' or 'quit' to exit the REPL and return to R.
>>> penguins=r.penguins
>>> penguins
species island bill_length_mm ... body_mass_g sex year
0 Adelie Torgersen 39.1 ... 3750 male 2007
1 Adelie Torgersen 39.5 ... 3800 female 2007
...
However, when I try to render this document, it errors this:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [1], in <cell line: 2>()
1 # Python chunk
----> 2 penguins=r.penguins
3 penguins
NameError: name 'r' is not defined
According to RMarkdown documentation, nothing else is required (so no e.g. rpy2
).
I try to add library(reticulate)
or reticulate::repl_python()
in the R chunk but it doesn't solve the issue.
Note: I'm aware of an old unanswered similar question for RMarkdown.
Thanks!