网状-在Rmarkdown中运行python块 [英] Reticulate - Running python chunks in Rmarkdown

查看:119
本文介绍了网状-在Rmarkdown中运行python块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我遗漏了一些东西,但是如果以下代码是我的Rmd文件的内容

Maybe I'm missing something, but if the following code is the content of my Rmd file

```{r}
library(reticulate)
use_virtualenv("r-reticulate")
py_available(TRUE)
```
```{python}
a = 7
print(a)
```
```{r}
py$a
```

当我编织文件时,最后一块的输出为7(按预期).另一方面,单击Rstudio中的全部运行按钮(或一个一个地运行块),最后一个块在NULL上产生.

when I Knit the file, the output for the last chunk is 7 (as expected). On the other hand, clicking the run all button in Rstudio (or running chunks one by one), results on NULL for the last chunk.

R笔记本示例相比 >在python块中应该使py$flights可用于R,但这似乎并非如此.

Comparing with the R notebook example it seems like assigning something to flights in the python chunk should make py$flights available for R, but that doesn't seem the case.

问题:

  1. 是否有办法从R中访问先前运行(而非编织)的Python块中创建的变量?如何导出"到R在python块中创建的变量?
  2. 了解我单击Rmarkdown文件的python块中的运行按钮时会发生什么情况?

编辑:好的,所以在这里看到第一个答案之后,我确实将knitr和rmarkdown都更新到了最新版本,但是仍然存在相同的问题. 我在文件中添加了py_available(TRUE)以确保对其进行了初始化,但是,最后一个块在编织时仍会生成7,但一个接一个地运行块会导致

EDIT: Ok so after seeing the first answers here, I did update both knitr and rmarkdown to the latest version, but still had the same problem. I added py_available(TRUE) to my file to make sure it was initialized, still, last chunk results in 7 when knitted, but running chunks one-by-one results in

> py$a
Error in py_get_attr_impl(x, name, silent) : 
  AttributeError: 'module' object has no attribute 'a'

问题是:在R环境中,在python块中为a分配值对py$a没有任何作用.也许R和python之间的这种共享"环境不是软件包应该如何工作的?另外,有关其他信息

The problem is: Assigning a value to a in the python chunk isn't doing anything to py$a in the R environment. Maybe this "shared" environment between R and python isn't how the package is supposed to work? Also, for some extra information

> py_config()
python:         /usr/bin/python
libpython:      /usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
pythonhome:     /usr:/usr
version:        2.7.14 (default, Sep 23 2017, 22:06:14)  [GCC 7.2.0]
numpy:          /usr/lib/python2.7/dist-packages/numpy
numpy_version:  1.12.1

python versions found: 
 /usr/bin/python
 /usr/bin/python3

推荐答案

Rmarkdown/knitr:

运行块:

到目前为止,不支持在不编织文档的情况下运行块.参见此处: https://github.com/yihui/knitr/issues/1440 网状结构在RMarkdown中的R/Python单元格或Python/Python单元格之间不共享状态.

Running the chunks without knitting the document is not supported so far. See here: https://github.com/yihui/knitr/issues/1440 or Reticulate not sharing state between R/Python cells or Python/Python cells in RMarkdown.

Freguglia的变通办法:

解决方法是将python块转换为R块,然后将全部内容包装在py_run_string()函数中,因此您可以通过py $ variable_name从R中访问在该代码段中分配的任何内容."

"Workaround is to turn python chunks into R chunks and just wrap the whole content in the py_run_string() function, so whatever you assign in that piece of code is accessible from R by py$variable_name."

编织文档:

一种方法是按照上面的建议升级knitr,但是您不必这样做,也不需要RStudio的日常构建.

One way is to upgrade knitr as suggested above, but you dont have to and you also dont need RStudio daily build.

如果您的knitr版本低于1.18,则可以包括:

If you have a version of knitr prior to 1.18, you can include:

```{r setup, include = FALSE} knitr::knit_engines$set(python = reticulate::eng_python) ``` ,请参见此处: https://rstudio.github .io/reticulate/articles/r_markdown.html#engine-setup .

```{r setup, include = FALSE} knitr::knit_engines$set(python = reticulate::eng_python) ``` , see here: https://rstudio.github.io/reticulate/articles/r_markdown.html#engine-setup.

Python:

如果它不起作用,请确保python连接在rmarmdown/knitr外部运行: py_run_string("x = 10"); py$x.

If it doesnt work ensure the python connection is running outside of rmarmdown/knitr: py_run_string("x = 10"); py$x.

如果仍然无法使用,则应检查: py_available()py_numpy_available().

In case that also doesnt work, you should check: py_available() and py_numpy_available().

如果它返回FALSE:尝试使用py_available(TRUE)对其进行初始化.

If it returns FALSE: Try to initialize it with: py_available(TRUE).

如果还是不行,请检查您的配置: py_config()

If that´s still a no - check your config: py_config()

它将为您提供有关该问题的进一步提示:

It will give you further hints on the problem:

对我来说,例子是:R和python的不同位版本(32 vs 64),或者以某种方式我同时安装了Python2.7和Anaconda时遇到了麻烦.

Examples for me were: different bit versions of R and python (32 vs 64) or somehow i ran into trouble having installed both Python2.7 and seperately Anaconda.

这篇关于网状-在Rmarkdown中运行python块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆