【问题标题】:Problem using knitter with reticulate package in rstudio with matplotlib在带有 matplotlib 的 rstudio 中使用带有网状包的 knitter 时出现问题
【发布时间】:2020-04-22 11:44:19
【问题描述】:

要详细说明标题,

我正在使用 rstudio 制作一个包含 r 和 python 代码的 rmarkdown 文件。我的配置如下:

Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6         rstudioapi_0.11      knitr_1.28           magrittr_1.5         rappdirs_0.3.1       tidyselect_1.0.0    
 [7] munsell_0.5.0        lattice_0.20-38      colorspace_1.4-1     R6_2.4.1             rlang_0.4.5          dplyr_0.8.5         
[13] tools_3.6.3          grid_3.6.3           gtable_0.3.0         xfun_0.12            htmltools_0.4.0      yaml_2.2.1          
[19] assertthat_0.2.1     digest_0.6.25        tibble_2.1.3         lifecycle_0.2.0      crayon_1.3.4         Matrix_1.2-18       
[25] purrr_0.3.3          ggplot2_3.3.0        rsconnect_0.8.16     glue_1.3.2           evaluate_0.14        rmarkdown_2.1       
[31] compiler_3.6.3       pillar_1.4.3         scales_1.1.0         jsonlite_1.6.1       reticulate_1.15-9000 pkgconfig_2.0.3 

我尝试运行的 python 块是:

import scipy as sp
import numpy as np
import pandas as pd


df = pd.read_csv("D:/03 PhD Edinburgh related/OneDrive/OneDrive - University of Edinburgh/00 PhD/000 PhD Data/01 Project I Chr Hansen/20200421_analysis_pp16013/20200420_pp16013_analysis.csv")

plt.scatter(df['OD'].values, df['osmolarity.mOSM'].values,
            c=df['hydrophobicity'].values,cmap='magma')
plt.xlabel('OD')
plt.ylabel('osmolarity')
plt.title('osmolarity as function of OD and Hydrophobicity')

cbar = plt.colorbar()
cbar.set_label('Hydrophobicity', rotation=270)

plt.show()

我可以在 rStudio 中正常运行 python 块并按预期显示图形。

当我尝试使用 knitter 制作文档时,它会到达 python 块并给出错误:

"This application failed to start because it could not find or load 
the Qt platform plugin "windows" in "", 
reinstalling the application may fix this problem."

这与使用matplotlib的部分特别相关。我可以创建和显示 Pandas 数据框。

我已经尝试安装最新版本的github reticulate。

提前感谢您的帮助。

【问题讨论】:

  • 您是否尝试用谷歌搜索此错误消息?我找到了this
  • 是的,谷歌透露它很可能与网状包有关。我按照说明通过直接从 github 安装包来更新到最新版本。
  • 但是我提供的链接谈到了发生此错误时要修改的 PATH。

标签: r knitr reticulate


【解决方案1】:

在我的 rmarkdown 文件中,在设置了正在使用的 conda 环境的插件路径后,它就可以正常工作了。您可以在将 matplotlib 用于 python 代码块之前添加以下 r 代码块。

library(reticulate)
py_run_string("import os as os")
py_run_string("os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:/Users/[UserID]/Anaconda3/envs/[EnvironmentName]/Library/plugins/platforms'")

【讨论】:

  • 嗨,谢谢,我觉得这是朝着正确的方向发展。我尝试使用我安装的python:C:/Users/mzurb/AppData/Local/Programs/Python/Python38-32/python.exe 这不起作用你能告诉我这个平台文件夹是什么,因为我没有anaconda3吗?
  • 在我的平台文件夹中,我可以找到“qwindows.dll”文件。也许,您可以尝试在您的安装文件夹周围找到该文件,如果可以找到包含该文件的文件夹,请指向该文件夹?
【解决方案2】:

我已经设法在另一个堆栈溢出答案中找到了问题,这是重复的。

答案请看这里。

https://stackoverflow.com/a/50711837/11076186

我基本上只是保存我的python图,然后将保存的python图加载到r中。

【讨论】:

    猜你喜欢
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 2020-07-16
    • 2021-09-08
    • 1970-01-01
    相关资源
    最近更新 更多