Maybe I'm missing something, but if the following code is the content of my Rmd file
也许我错过了一些东西,但如果下面的代码是我的Rmd文件的内容
```{r}
library(reticulate)
use_virtualenv("r-reticulate")
py_available(TRUE)
```
```{python}
a = 7
print(a)
```
```{r}
py$a
```
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.
当我编织文件时,最后一个块的输出是7(如预期的那样)。另一方面,单击Rstudio中的“全部运行”按钮(或逐个运行块),最后一个块的结果为NULL。
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.
与R笔记本示例相比,似乎为python块中的航班分配了一些东西,应该为R提供py $航班,但事实并非如此。
Questions:
问题:
- Is there a way to access from R a variable created in a Python chunk previously ran (not knit)? How to "export" to R a variable created within a python chunk?
- 有没有办法从R访问一个先前运行过的Python块中创建的变量(不是编织)?如何“导出”R在python块中创建的变量?
- What is a good reference to understand what happens when I click the run button in a python chunk of a Rmarkdown file?
- 什么是一个很好的参考,以了解当我点击Rmarkdown文件的python块中的运行按钮时会发生什么?
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
编辑:好的,所以在看到第一个答案后,我确实更新了knitr和rmarkdown到最新版本,但仍然有同样的问题。我将py_available(TRUE)添加到我的文件中以确保它已被初始化,但是,当针织时,最后一个块结果为7,但是逐个运行块会导致
> py$a
Error in py_get_attr_impl(x, name, silent) :
AttributeError: 'module' object has no attribute 'a'
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块中的a赋值不会对R环境中的py $ a做任何事情。也许R和python之间的这个“共享”环境不是这个包应该如何工作的?另外,还有一些额外的信息
> 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
2 个解决方案
#1
1
Rmarkdown / knitr:
Rmarkdown / knitr:
Running the chunks:
运行块:
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.
到目前为止,不支持在不编织文档的情况下运行块。请参阅此处:https://github.com/yihui/knitr/issues/1440或网格化不在RM / RM中的R / Python单元格或Python / Python单元格之间共享状态。
Edit: Workaround by Freguglia:
编辑:Freguglia的解决方法:
"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."
“解决方法是将python块转换为R块,并将整个内容包装在py_run_string()函数中,因此无论您在该段代码中分配的是什么,都可以通过py $ variable_name从R中访问。”
Knitting the document:
编织文件:
One way is to upgrade knitr
as suggested above, but you dont have to and you also dont need RStudio daily build.
一种方法是按照上面的建议升级knitr,但是你没必要,而且你也不需要每天构建RStudio。
If you have a version of knitr prior to 1.18, you can include:
如果你有1.18之前的knitr版本,你可以包括:
```{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.
```{r setup,include = FALSE} knitr :: knit_engines $ set(python = reticulate :: eng_python)```,见这里:https://rstudio.github.io/reticulate/articles/r_markdown.html#发动机设置。
Python:
蟒蛇:
If it doesnt work ensure the python connection is running outside of rmarmdown/knitr: py_run_string("x = 10"); py$x
.
如果它不起作用,请确保python连接在rmarmdown / knitr之外运行:py_run_string(“x = 10”); PY $ X。
In case that also doesnt work, you should check: py_available()
and py_numpy_available()
.
如果也不起作用,您应该检查:py_available()和py_numpy_available()。
If it returns FALSE
: Try to initialize it with: py_available(TRUE)
.
如果返回FALSE:尝试使用以下命令初始化它:py_available(TRUE)。
If that´s still a no - check your config: py_config()
如果仍然没有 - 检查你的配置:py_config()
It will give you further hints on the problem:
它将为您提供有关该问题的进一步提示:
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.
我的例子是:R和python的不同位版本(32对64)或者不知何故我遇到了安装Python2.7和单独的Anaconda的麻烦。
#2
3
You must use the Rstudio daily build (source) and upgrade knitr
, rmarkdown
to the latest version.
您必须使用Rstudio每日构建(源)并将knitr,rmarkdown升级到最新版本。
> packageVersion("rmarkdown")
[1] ‘1.9’
> packageVersion("knitr")
[1] ‘1.20’
#1
1
Rmarkdown / knitr:
Rmarkdown / knitr:
Running the chunks:
运行块:
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.
到目前为止,不支持在不编织文档的情况下运行块。请参阅此处:https://github.com/yihui/knitr/issues/1440或网格化不在RM / RM中的R / Python单元格或Python / Python单元格之间共享状态。
Edit: Workaround by Freguglia:
编辑:Freguglia的解决方法:
"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."
“解决方法是将python块转换为R块,并将整个内容包装在py_run_string()函数中,因此无论您在该段代码中分配的是什么,都可以通过py $ variable_name从R中访问。”
Knitting the document:
编织文件:
One way is to upgrade knitr
as suggested above, but you dont have to and you also dont need RStudio daily build.
一种方法是按照上面的建议升级knitr,但是你没必要,而且你也不需要每天构建RStudio。
If you have a version of knitr prior to 1.18, you can include:
如果你有1.18之前的knitr版本,你可以包括:
```{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.
```{r setup,include = FALSE} knitr :: knit_engines $ set(python = reticulate :: eng_python)```,见这里:https://rstudio.github.io/reticulate/articles/r_markdown.html#发动机设置。
Python:
蟒蛇:
If it doesnt work ensure the python connection is running outside of rmarmdown/knitr: py_run_string("x = 10"); py$x
.
如果它不起作用,请确保python连接在rmarmdown / knitr之外运行:py_run_string(“x = 10”); PY $ X。
In case that also doesnt work, you should check: py_available()
and py_numpy_available()
.
如果也不起作用,您应该检查:py_available()和py_numpy_available()。
If it returns FALSE
: Try to initialize it with: py_available(TRUE)
.
如果返回FALSE:尝试使用以下命令初始化它:py_available(TRUE)。
If that´s still a no - check your config: py_config()
如果仍然没有 - 检查你的配置:py_config()
It will give you further hints on the problem:
它将为您提供有关该问题的进一步提示:
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.
我的例子是:R和python的不同位版本(32对64)或者不知何故我遇到了安装Python2.7和单独的Anaconda的麻烦。
#2
3
You must use the Rstudio daily build (source) and upgrade knitr
, rmarkdown
to the latest version.
您必须使用Rstudio每日构建(源)并将knitr,rmarkdown升级到最新版本。
> packageVersion("rmarkdown")
[1] ‘1.9’
> packageVersion("knitr")
[1] ‘1.20’