knitr:Python的代码外部化

2024-06-10 04:25:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用read_chunk()从外部源代码读取R代码。我想知道是否有类似的函数将Python代码读入主文档。你知道吗

我的.Rmd文档的一些摘录。你知道吗

R works, of course.

```{r test-r, engine='R'}
library(knitr)
set.seed(123)
rnorm(5)
```

Does **knitr** work with Python? Use the chunk option `engine='python'`:

```{r test-python, engine='python'}
x = 'hello, python world!'
print(x)
print(x.split(' '))
```

Tags: of函数代码文档testread源代码library
1条回答
网友
1楼 · 发布于 2024-06-10 04:25:19

当然,这适用于Python:

创建测试.py包含以下内容的文件:

## @knitr abc
print(1)

在您的RMD文件中:

```{r}
knitr::read_chunk('test.py')
```

```{r abc, engine='python'}
```

对我来说很好。你知道吗

相关问题 更多 >