'Call R object from Python with r. in a Quarto document
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!
Solution 1:[1]
Quarto have two engines for render, knitr and jupyter.A related document is here.
If we use:
---
title: "pandas"
format: html
---
```{r}
data("penguins", package = "palmerpenguins")
```
```{python}
penguins=r.penguins
penguins
```
The engine will be knitr. And while render, knitr will use reticulate
(R Interface to Python) to run python code chunk. In this process, knitr will do some magic things to convert the form like r.penguins
to the form of reticulate
. So the document will be successfully rendered. In other words, knitr made some adaptations to let us could easily run python code chunk with reticulate
, and if we don't use knitr engine, we can't use the form like r.penguins
.
Quarto use r run all code chunks (auto use r package
reticulate
to run python chunks) when it uses knitr engine.
And Quarto use python to run all code chunks when it uses jupyter (jupyter: python3) engine. If we want run r code, we must use module (such as rpy2) in python chunk (not r chunk, codes in r chunk will not be run).
We can also use r to run all code chunks by setting
jupyter: ir
(if we have installed IRkernel).But codes in python chunk will not be run. We must use package (such as reticulate) in r chunk to run python code.
This is my understanding. My English is not good, so if some sentence made you puzzled, we could discuss further.
Solution 2:[2]
I have no explanation, but removing the line jupyter: python3
in the YAML solved my issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gleko Ma |
Solution 2 | abichat |