Starting with R Markdown flexdashboard

This is a rather straightforward example using R Markdown, flexdashboard and an Excel datasource. 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

RMarkdownNew

 

If everything is going as planned, then you should be able to select the ‘Flex Dashboard’ template from the R Markdown templates.

Flexdashboard

 

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)`.

 

Economic Indicators R Flex Dashboard

 

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

3 comments

  1. Angela Moss · · Reply

    Hi Jonathan
    I have worked through this example using the code and the blog. It has been really helpful thank you to get me started. The only bit I had to work out was how to get the insight section to work – I ended up adding “` before and after each r min, r max and r mean statement. The only difference to your dash board is that the dates and mean are in red text. So not sure whether my solution is correct or not, or how would I change the text colour.
    Many thanks for your help.

    Like

  2. Angela,

    Took me a little-bit to understand what was happening. When viewing the code on GitHub the tick marks (`) are not displayed unless you click the ‘Raw’ button to view the raw code. If you have GIT installed on your machine you could also start a new RStudio project by connecting to an existing GitHub repository. Let me know if you need any help with this… In Markdown the Text colour can be changed using LaTex (PDF) or CSS/HTML. Is your code exactly the same? If so, I will have to do some digging to see what dependency script/css files are being used.

    Like

  3. What’s up,I log on to your new stuff named “Starting with R Markdown flexdashboard | Stochastic Coder” on a regular basis.Your story-telling style is witty, keep up the good work! And you can look our website about proxy server list.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: