2

EDIT Found solution. Plot Python Plots Inline

I have been making a Rmarkdown notebook to take notes on my python studies. Within RStudio I have been able to knit the document containing my python code to HTML with no problem until I started plotting data using matplotlib. The curious part is that the plots are generated correctly within the code chunks. However, after knitting, it spits out an error every time at 80%.

Here is my sample code:

---
title: "Python Plot"
output: html_document
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE)
library(reticulate) #Allows for Python to be run in chunks
```


```{python, eval=F}
import numpy as np
trees = np.array(r.trees) #Imported an internal R dataset. It got rid of headers and first row. Don't know how to deal with that right now.
type(trees)
np.shape(trees)
print(trees[1:6,:])

import matplotlib.pyplot as plt
plt.plot(trees[:,0], trees[:,1])
plt.show()
plt.clf() #Reset plot surface
```

Again, this plot comes out just fine when processing within the chunk, but does not knit. The error message says,

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

I have uninstalled and reinstalled both Rstudio and Python and continue to have the same result. I find it odd that it works within the chunk but not to knit to HTML. All my other python code knits just fine.

I have

python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)]

Rstudio Version 1.2.1335.

I've read other solutions. I believe that the libEGL.dll is in the same place as all the other QT5*.dll.

Scott Hunter
  • 254
  • 2
  • 14
  • Try Googling that error message: *This application failed to start because it could not find or load the Qt platform plugin "windows"*. Various hits may be useful. – Parfait Jun 29 '19 at 22:01
  • Thanks, I did, but some didn't apply and others seemed way above my level. I just don't see why it works within the envirionment. – Scott Hunter Jun 29 '19 at 23:13
  • Possible duplicate of [Qt 5.1.1: Application failed to start because platform plugin "windows" is missing](https://stackoverflow.com/questions/20495620/qt-5-1-1-application-failed-to-start-because-platform-plugin-windows-is-missi) – Ken White Jun 30 '19 at 03:49
  • @Ken, it could be a duplicate. I just spent the last hour or more reading and trying to do things, but I'll just have to admit my incompetence. I don't understand it. I don't know if it's duplicate. I don't think so because I believe all appropriate .dll are in the correct place. I don't know why it will knit the other python code without error. I don't know why it plots the pythod code within chunk. It just won't knit any code with python plots. I appreciate links, but I think I'll just give up. – Scott Hunter Jun 30 '19 at 21:23

3 Answers3

3

I found an answer here by Kevin Arseneau. Plot Python Plots Inline

I would not call this a duplicate because the problem was different, but the solution worked for both problems.

What is needed is to add the following code:

import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = '/path/to/Anaconda3/Library/plugins/platforms'

Here is my updated working code. It is similar to original question and updated with a python chunk for imports that Bryan suggested.

---
title: "Python Plot"
output: html_document
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE)
library(reticulate) #Allows for Python to be run in chunks
```

```{python import}
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = '/path/to/Anaconda3/Library/plugins/platforms'
import numpy as np
import matplotlib.pyplot as plt


```{python, eval=TRUE}

trees = np.array(r.trees) #Imported an internal R dataset. It got rid of headers and first row. Don't know how to deal with that right now.
type(trees)
np.shape(trees)
print(trees[1:6,:])


plt.plot(trees[:,0], trees[:,1])
plt.show()
plt.clf() #Reset plot surface
```
Scott Hunter
  • 254
  • 2
  • 14
0

In your import libraries, you have to install and import PyQT5 into your Python environment. So for example, my first chunks look like the following, first line of # Base Libraries is import PyQt5:

---
title: "Cancellations TS"
author: "Bryan Butler"
date: "7/1/2019"
output:
    html_document:
    toc: false
    toc_depth: 1
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, cache.lazy = FALSE)
```

# <strong>Time Series of Auto Policies</strong> {.tabset .tabset-fade .tabset-pills}


<style>
  .main-container {
    max-width: 1200px !important;
    margin-left: auto;
    margin-right: auto;
  }
</style>

{r, loadPython}
library(reticulate)
use_condaenv('timeseries')


## Load Python
{python importLibaries}

# Base libraries
import PyQt5
import pandas as pd
from pandas import Series, DataFrame
from pandas.plotting import lag_plot


import numpy as np
import pyodbc
Bryan Butler
  • 1,750
  • 1
  • 19
  • 19
  • Bryan, I tried but still couldn't get plots to knit. I could not import PyQt5 before your advice. I can now. Still no luck. Will you please copy my original code above and see if you can knit the document to html? – Scott Hunter Jul 02 '19 at 16:29
0

I was able to run your code with some modifications. I broke the chunks up for error checking. You need to import numpy as np, and I added others. Here is the code that I got to work. Also, I use conda virtual environments so that the Python environment is exact. This is what worked:

---
title: "test"
author: "Bryan Butler"
date: "7/2/2019"
output: html_document
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE)
library(reticulate) #Allows for Python to be run in chunks
use_condaenv('timeseries')
```

```{python import}
import PyQt5
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt
```

```{python, test}
trees = np.array(r.trees) #Imported an internal R dataset. It got rid of headers and first row. Don't know how to deal with that right now.
type(trees)
np.shape(trees)
print(trees[1:6,:])
```

```{python plot}
plt.plot(trees[:,0], trees[:,1])
plt.show()
plt.clf() #Reset plot surface
```
Bryan Butler
  • 1,750
  • 1
  • 19
  • 19
  • Thanks Bryan for your efforts. I still couldn't get it to work. I was able to test on other's machines and still no luck. Then I found an answer. See below. – Scott Hunter Jul 03 '19 at 14:45