我正在尝试在 Quarto 文档中从 Python 中调用 R 对象:
当我在RStudio中逐个执行这些代码块时,一切都正常:
然而,当我尝试呈现这个文档时,它会出现错误:
根据RMarkdown文档,不需要其他东西(例如
我尝试在R块中添加
注意:我知道有一个类似的旧未答复的问题,也是关于RMarkdown的。
谢谢!
---
title: "pandas"
format: html
jupyter: python3
---
```{r}
data("penguins", package = "palmerpenguins")
```
```{python}
penguins=r.penguins
penguins
```
当我在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
...
然而,当我尝试呈现这个文档时,它会出现错误:
。
---------------------------------------------------------------------------
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
根据RMarkdown文档,不需要其他东西(例如
rpy2
)。我尝试在R块中添加
library(reticulate)
或reticulate::repl_python()
,但这并没有解决问题。注意:我知道有一个类似的旧未答复的问题,也是关于RMarkdown的。
谢谢!
penguins
替换为mtcars
后运行了您的代码(保存为test-reticulate.Rmd
),并且渲染时没有收到任何错误消息。我使用以下代码来渲染文档:library(rmarkdown); render("test-reticulate.Rmd")
。我的平台分别来自于sessionInfo()
和sys.version
。R:R version 4.1.3 (2022-03-10),Platform: x86_64-conda-linux-gnu (64-bit),Running under: Amazon Linux AMI 2018.03
;Python:'3.10.4 | packaged by conda-forge | (main, Mar 24 2022, 17:51:59) [GCC 10.3.0]'
。 R包版本:reticulate_1.24 rmarkdown_2.13
。 - mastropi