代码之家  ›  专栏  ›  技术社区  ›  RockScience

rmarkdown中的蟒蛇(网状)

  •  1
  • RockScience  · 技术社区  · 8 年前

    ```{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()
    ```
    

    但是,在编写文档时会出现此错误:(请注意,在运行第二个块时会发生此错误)

    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

    我有knitr版本1.20,高于1.18,因此引擎配置应该是自动的。

    1 回复  |  直到 8 年前
        1
  •  1
  •   strangeloop    7 年前

    这可能是由于使用低于1.2的RStudio版本造成的。它很好地隐藏在 reticulate 页面,但实际上导入Python包和运行Python块的某些方面似乎是 仅在RStudio 1.2及更高版本中受支持 也就是说,不是当前的RStudio稳定版,而是需要单独下载和安装的预览版。

    Here's what they write 在小插曲中:

    请注意 RStudio v1.2 preview release RStudio IDE Tools for reticulate 文章了解更多详细信息。

    可能正因为如此,在RStudio 1.1.53中运行代码时,我会像您一样出现“ModuleNotFound”错误,它们会阻止编织。

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

    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")
    >>> 
    

    test.png