Skip to content

Instantly share code, notes, and snippets.

@webbedfeet
Last active December 11, 2020 06:10
Show Gist options
  • Save webbedfeet/72b6203b0e6c11cdb15298ea6a04b149 to your computer and use it in GitHub Desktop.
Save webbedfeet/72b6203b0e6c11cdb15298ea6a04b149 to your computer and use it in GitHub Desktop.
Including python-based graphics in flexdashboard
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly
import plotly.express as px
iris = sns.load_dataset('iris')
fig, ax = plt.subplots()
sns.scatterplot(data = iris, x = 'sepal_length', y = 'sepal_width',
hue = 'species',
ax = ax)
ax.set_xlabel('Sepal Length')
ax.set_ylabel('Sepal Width')
ax.legend(title='Species')
fig.savefig('plot1.png')
fig2 = px.scatter(iris, x= 'sepal_length', y = 'sepal_width', color='species',
labels = {
'sepal_length': 'Sepal Length',
'sepal_width' : 'Sepal Width',
'species' : "Species"
})
plotly.offline.plot(fig2, filename = 'plot2_1.html', auto_open=False,)
# fig2.write_html('plot2_1.html')
---
title: "Example dashboard"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
html_document: default
---
```{r setup, include=FALSE}
library(flexdashboard)
```
```{css}
iframe {
display: block;
margin: 0 auto;
}
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{python, eval=FALSE}
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
iris = sns.load_dataset('iris')
fig, ax = plt.subplots()
sns.scatterplot(data = iris, x = 'sepal_length', y = 'sepal_width',
hue = 'species',
ax = ax)
ax.set_xlabel('Sepal Length')
ax.set_ylabel('Sepal Width')
ax.legend(title='Species')
fig.savefig('plot1.png')
```
```{r, echo=FALSE, fig.width=5, fig.height=5}
knitr::include_graphics('plot1.png')
```
### Chart B
```{python, eval = FALSE}
import plotly
import plotly.express as px
fig2 = px.scatter(iris, x= 'sepal_length', y = 'sepal_width', color='species',
labels = {
'sepal_length': 'Sepal Length',
'sepal_width' : 'Sepal Width',
'species' : "Species"
})
plotly.offline.plot(fig2, filename = 'plot2_1.html', auto_open=False,)
```
```{r, echo=FALSE}
knitr::include_url('plot2_1.html', height='400px')
```
---
title: "Example dashboard"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
html_document: default
---
```{r setup, include=FALSE}
library(flexdashboard)
```
```{css}
iframe {
display: block;
margin: 0 auto;
}
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{python, eval=FALSE}
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
iris = sns.load_dataset('iris')
fig, ax = plt.subplots()
sns.scatterplot(data = iris, x = 'sepal_length', y = 'sepal_width',
hue = 'species',
ax = ax)
ax.set_xlabel('Sepal Length')
ax.set_ylabel('Sepal Width')
ax.legend(title='Species')
fig.savefig('plot1.png')
```
<img src="plot1.png">
### Chart B
```{python, eval = FALSE}
import plotly
import plotly.express as px
fig2 = px.scatter(iris, x= 'sepal_length', y = 'sepal_width', color='species',
labels = {
'sepal_length': 'Sepal Length',
'sepal_width' : 'Sepal Width',
'species' : "Species"
})
plotly.offline.plot(fig2, filename = 'plot2_1.html', auto_open=False,)
```
<iframe src="plot2_1.html', height=400></iframe>
@webbedfeet
Copy link
Author

To create the dashboard, you would first run the code in testdash.py, then you would knit either testdash.Rmd or testdash2.Rmd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment