This is a rather straightforward example using R Markdown, flexdashboard and an Excel data source. No Shiny, no fancy maps, just a simple dashboards and some ggplot graphs. For this example we will use U.S. Economic Indicators from the St. Louis Federal Reserve FRED . The code and data set are available on github; the link is provided towards the end of the post.
Flexdashboard from a Template
Please make sure you have installed flexdashboard before proceeding.
devtools::install_github('rstudio/flexdashboard')
Once flexdashboard has been installed, let’s create a new R Markdown file in RStudio.
File -> New File -> R Markdown
If everything is going as planned, then you should be able to select the ‘Flex Dashboard’ template from the R Markdown templates.
You should now have a file with generated code (as shown below), but it does not do anything super cool as of yet.
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
```
Column {data-width=650}
-----------------------------------------------------
### Chart A
```{r}
```
Column {data-width=350}
------------------------------------------------------
### Chart B
```{r}
```
### Chart C
```{r}
```
-----------------------------------------------
Making the Dashboard
First things first, let’s make the title meaningful. The orientation and layout can remain unchanged for this example.
---
title: "Economic Indicators"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
To generate the graphs we need data.Use XLSX to read the economic data stored in the fredgraph.xls.
```{r setup, include=FALSE}
library(flexdashboard)
library(xlsx)
library(ggplot2)
#Read US GDP Data Set from FED Excel File
dtGDP <- read.xlsx('fredgraph.xls',sheetName = 'GDP')
#Read US Unemployment Rate and ISM PCI Index Data Set from FED Excel File
dtOtherIndicators <- read.xlsx('fredgraph.xls',sheetName = 'Sheet2')
```
The dashboard will have two columns and two rows. Since the orientation is set to columns, every ‘chunk’ under a column header will display as a new row under that column.
Inside the first column will be two rows, one containing a graph of the U.S. Real GPD and the other a graph of the U.S. Unemployment Rate. Using the R Markdown fig.width parameter I am able to ‘widen’ the ggplot output to match the width of the column. This was not necessary when using plotOutput in my Shiny project RMarkdown flexdashboard – Test Drive .
Column {data-width=650}
------------------------------------------------------
### GDP Quarterly Percent Changed - Seasonaly Adjusted
```{r fig.width=10}
ggplot(dtGDP,aes(x=Date,y=RGDP)) +
geom_line(color="blue") +
ggtitle("") +
xlab("") +
ylab("") +
theme(axis.text.x =element_text(size=12),axis.text.y =element_text(size=12))
```
### Civilian Unemployment Rate, Percent, Monthly, Seasonally Adjusted
```{r fig.width=10}
ggplot(dtOtherIndicators,aes(x=Date,y=UNRATE)) +
geom_line(color="blue") +
ggtitle("") +
xlab("") +
ylab("") +
theme(axis.text.x =element_text(size=12), axis.text.y =element_text(size=12))
```
The second column will display the graph for the ISM PCI Index and then I included some inline R with text.
Column {data-width=350}
-----------------------------------------------------
### ISM Manufacturing: PMI Composite Index
```{r fig.width=5, fig.height=5}
# Create Line Graph To Display Time Series ISM PCI Index
ggplot(dtOtherIndicators,aes(x=Date,y=ISM.PMI)) +
geom_line(color="blue") +
ggtitle("") +
xlab("") +
ylab("") +
theme(axis.text.x =element_text(size=12),axis.text.y =element_text(size=12))
```
### Insight
Average Unemployment Rate from `r min(dtOtherIndicators$Date)` to `r max(dtOtherIndicators$Date)` is `r mean(dtOtherIndicators$UNRATE)`.
I posted the full code on github.
Github flexdashboard Economy Indicators
I hope this simple example can help get you up-and-running quickly with R Markdown and flexdashboard.
Please let me know if you have any questions or problems.
Jonathan
Leave a Reply