将python与knitr一起使用

将python与knitr一起使用,python,r,markdown,knitr,Python,R,Markdown,Knitr,我想将python与knitr一起使用。然而,python块似乎是单独计算的,块之间的变量定义丢失了 如何解决这个问题 最简单的例子: test.pymd test.md 是的,事实上,knitr目前无法为R以外的语言评估扩展到多个块的代码。解决方案不是使用knitr,而是使用pweave。对源文件的修改很少: test.mdw 网站上有一条说明,如果使用python3安装pip,安装将失败。不过,我在跑步时一点问题都没有: pip install pweave pip install mark

我想将python与knitr一起使用。然而,python块似乎是单独计算的,块之间的变量定义丢失了

如何解决这个问题

最简单的例子:

test.pymd test.md
是的,事实上,knitr目前无法为R以外的语言评估扩展到多个块的代码。解决方案不是使用knitr,而是使用pweave。对源文件的修改很少:

test.mdw 网站上有一条说明,如果使用python3安装pip,安装将失败。不过,我在跑步时一点问题都没有:

pip install pweave
pip install markdown

也许,这只是一个古老的注释。

以下是在knittr块中运行python代码的两种方法的简单示例:

---
title: "Works with python"
output: github_document
---

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

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

Or use the syntax ```` ```{python} ````:

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

我是从

获得的,您已经尝试过这个链接了吗?这将在不久的将来得到改善。目前,大多数非R组块彼此独立,变量不会持久。@YihuiXie这里有更新吗?我想在最近的一次研究中,我记得一些类似的东西releases@MichaelChirico对:
#! /usr/bin/Rscript --vanilla

args <- commandArgs(TRUE)

if(length(args) < 1) {
    message("Need arguments in the format: %.pymd [%.md]")
    q(status=1)
}

name <- substr(args[1],1,nchar(args[1])-4)

if(length(args) < 2) {
    args[2] <- paste0(name,".md")
}

library(knitr)
opts_chunk$set(engine = 'python')

res <- try(knit(args[1], args[2]))

if(inherits(res, "try-error")) {
    message("Could not successfully knit RMD (or PYMD) to MD")
    q(status=1)
} else q()
./knit2py.r test.pymd test.md 
---
title: "Minimal example"
---

With a print statement.

<<>>=
x = 'Hello, Python World!'
print(x)
@

Without a print statement.

<<>>=
print(x)
@

The end.
pweave -f pandoc test.mdw
pip install pweave
pip install markdown
---
title: "Works with python"
output: github_document
---

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

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

Or use the syntax ```` ```{python} ````:

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