rmarkdown中的python(网状) [英] python in rmarkdown (reticulate)

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

问题描述

我正在尝试在rmarkdown文档中添加一个python块.我安装了网状软件包,然后这是我的文档:

I am trying to add a python chunk in a rmarkdown document. I installed package reticulate then here is my document:

```{r, message=FALSE, warning=FALSE, echo = FALSE}
  library(reticulate)
```

```{python, echo = FALSE, eval = FALSE}
a=1
a
#import numpy as np
#import matplotlib.pyplot as plt

## evenly sampled time at 200ms intervals
#t = np.arange(0., 5., 0.2)

## red dashes, blue squares and green triangles
#plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
#plt.show()
```

但是,我在编织文档时遇到此错误:(请注意,在运行第二个块时会发生该错误)

However I get this error when knitting the document: (note that the error happens when running the second chunk)

label: unnamed-chunk-1 (with options) 
List of 3
 $ message: logi FALSE
 $ warning: logi FALSE
 $ echo   : logi FALSE


  |                                                                       
  |....                                                             |   6%
  ordinary text without R code


  |                                                                       
  |......                                                           |   9%
label: unnamed-chunk-2 (with options) 
List of 3
 $ echo  : logi FALSE
 $ eval  : logi FALSE
 $ engine: chr "python"

Error in py_module_import(module, convert = convert) : 
  ModuleNotFoundError: No module named 'rpytools'
Calls: <Anonymous> ... remap_output_streams -> import -> py_module_import -> .Call

此外,我还没有在 https://github.com/rstudio/reticulate上找到任何与此相关的信息 https://rstudio.github.io/reticulate/articles/r_markdown.html

Also adding that I did not find any information on this on https://github.com/rstudio/reticulate and https://rstudio.github.io/reticulate/articles/r_markdown.html

我的Knitr版本为1.20,高于1.18,因此引擎配置应该是自动的.

I have knitr version 1.20, which is above 1.18 hence engine configuration should be automatic.

推荐答案

这可能是由于使用低于1.2的RStudio版本引起的.在reticulate页上它是隐藏的,但实际上似乎仅在RStudio 1.2及更高版本中支持导入Python程序包和运行Python块的某些方面 –也就是说,当前的RStudio不稳定发布,但必须单独下载和安装预览.

This is likely caused by using an RStudio version lower than 1.2. It's well-hidden on the reticulate pages, but actually some aspects of importing Python packages and running Python chunks seem to be supported only in RStudio version 1.2 and up — that is, not the current RStudio stable release, but the preview that you have to download and install separately.

这是他们在小插图中写的内容:

请注意, RStudio v1.2预览版本包括对使用网状结构的支持在R Notebooks中执行Python块.有关更多详细信息,请参见 RStudio IDE网状工具文章.

Note that the RStudio v1.2 preview release includes support for using reticulate to execute Python chunks within R Notebooks. See the RStudio IDE Tools for reticulate article for additional details.

可能是因为这样,当在RStudio 1.1.53中运行代码时,我像您一样遇到"ModuleNotFound"错误,并且它们阻止了编织.

Probably because of this, when running your code in RStudio 1.1.53, I get "ModuleNotFound" errors like you do, and they prevent knitting.

在RStudio 1.2.1139预览版中运行它时,一切都应该如此:

When running it in the RStudio 1.2.1139 preview release, all is as it should be:

Reticulate 1.10 REPL -- A Python interpreter in R.
>>> a=1
>>> a
1
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> 
>>> ## evenly sampled time at 200ms intervals
>>> t = np.arange(0., 5., 0.2)
>>> 
>>> ## red dashes, blue squares and green triangles
>>> plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
[<matplotlib.lines.Line2D object at 0x000000001C2EDBE0>, <matplotlib.lines.Line2D object at 0x000000001DA99978>, <matplotlib.lines.Line2D object at 0x000000001DA99D30>]
>>> plt.show()
>>> plt.savefig("test.png")
>>> 

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

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