Search code examples
rknitrreticulate

Problem using knitter with reticulate package in rstudio with matplotlib


To elaborate on the title,

I am using rstudio making an rmarkdown file that contains both r and python code. My configuration is below:

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 

The python chunk I try running is:

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

I can run the python chunk fine in rStudio and display the graph as expect.

When I try using knitter to make the document it gets to the python chunk and gives the error:

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

This is specifically related to the part using matplotlib. I can create and display the Pandas data frame.

I have tried installing the latest version of reticulate of github.

Thank you for any help in advance.


Solution

  • I have managed to find the issue in another stack overflow answer this is a duplicate.

    For answer please look here.

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

    I basically just save my python plot and then load the saved python plot in r.