简体   繁体   English

使用 r 从 Python 调用 R 对象。 在 Quarto 文档中

[英]Call R object from Python with r. in a Quarto document

I try to call an R object from Python inside a Quarto document:我尝试在 Quarto 文档中从 Python 调用 R 对象:

---
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:当我在 RStudio 中一一执行块时,一切正常:

> 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 eg rpy2 ).根据RMarkdown 文档,不需要其他任何东西(所以不需要例如rpy2 )。

I try to add library(reticulate) or reticulate::repl_python() in the R chunk but it doesn't solve the issue.我尝试在 R 块中添加library(reticulate)reticulate::repl_python()但它不能解决问题。

Note: I'm aware of an old unanswered similar question for RMarkdown.注意:我知道 RMarkdown 的一个旧的未回答的类似问题

Thanks!谢谢!

Quarto have two engines for render, knitr and jupyter.A related document is here . Quarto 有两个渲染引擎,knitr 和 jupyter。相关文档在这里

If we use:如果我们使用:

---
title: "pandas"
format: html
---

```{r}
data("penguins", package = "palmerpenguins")
```

```{python}
penguins=r.penguins
penguins
```

The engine will be knitr.引擎将是knitr。 And while render, knitr will use reticulate (R Interface to Python) to run python code chunk.在渲染时,knitr 将使用reticulate (R 接口到 Python)来运行 python 代码块。 In this process, knitr will do some magic things to convert the form like r.penguins to the form of reticulate .在这个过程中,knitr 会做一些神奇的事情,将r.penguins这样的形式转换为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 .换句话说,knitr 做了一些修改,让我们可以轻松地运行带有reticulate的 python 代码块,如果我们不使用 knitr 引擎,我们就不能使用像r.penguins这样的形式。

Quarto use r run all code chunks (auto use r package reticulate to run python chunks) when it uses knitr engine.使用 knitr 引擎时,Quarto 使用 r 运行所有代码块(自动使用 r 包reticulate运行 python 块)。

And Quarto use python to run all code chunks when it uses jupyter (jupyter: python3) engine.而Quarto在使用jupyter(jupyter:python3)引擎时使用python运行所有代码块。 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).如果我们想运行r代码,我们必须在python块中使用模块(例如rpy2)(不是r块,r块中的代码不会运行)。

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.我们也可以通过设置jupyter: ir来使用 r 运行所有代码块(如果我们已经安装了 IRkernel)。但是 python 块中的代码将不会运行。 We must use package (such as reticulate) in r chunk to run python code.我们必须在 r 块中使用包(例如网状)来运行 python 代码。

This is my understanding.这是我的理解。 My English is not good, so if some sentence made you puzzled, we could discuss further.我的英语不好,如果有一句话让你不解,我们可以进一步讨论。

我没有解释,但删除 YAML 中的jupyter: python3行解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM