knitr:Python的代码外化

时间:2017-03-14 11:54:21

标签: python r knitr

我使用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(' '))
```

1 个答案:

答案 0 :(得分:3)

当然这适用于Python:

创建一个包含以下内容的test.py文件:

## @knitr abc
print(1)

在您的RMD文件中:

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

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

对我来说很好。