Market Share by State
Pizza Market Growth by State
DCMINCNYSC
---
title: "Zaco Dashboard"
author: "JJK"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    source_code: embed
---
  
```{r setup, include=FALSE}

# # clear workspace
rm(list=ls())

# # set directory
setwd('/Users/jjking/Documents/R/AARUG')

# # library repository
library(magrittr)
library(dplyr)
library(tidyr)
library(tigris)
library(leaflet)
library(ggplot2)
library(plotly)
library(DT)

######################################################
# read data
######################################################

# # tigris states function for shapefile
states <- states(cb = TRUE)
# # limit to contiguous 48 + DC
states <- states[!states$STUSPS %in% c('AK', 'AS', 'HI', 'PR', 'GU', 'MP', 'VI'), ] 

# # dummy data for share of pizza and taco restaurants by quarter by state; random walk
df <- tibble(q = rep(seq(as.Date('2046/1/1'), as.Date('2050/12/31'), by='3 months'), 49),
             state = rep(states@data$STUSPS, 20),
             pizza = rnorm(20*49),
             tacos = rnorm(20*49)) %>%
  group_by(state) %>%
  arrange(q) %>%
  mutate(pizza = cumsum(pizza),
         tacos = cumsum(tacos))

# # standardize random walk
sdz <- function(x){((x-min(x))/(max(x)-min(x)))}
df$pizza %<>% sdz
df$tacos %<>% sdz

# # join most recent quarter data to shape
states@data %<>% 
  cbind(., 
        df %>% ungroup() %>% filter(q == max(df$q)) %>% select(pizza, tacos)) 

######################################################
# map addons
######################################################

# # store shape color
pal <- colorNumeric("Oranges", domain=seq(0,1,.1))
pal_leg <- colorNumeric("Oranges", domain=seq(0,100,10)) # for legend

# # set hover popup text
pop <- paste0(states$NAME,
                  "Pizza Share: ",
                  sapply(states$pizza, function(x) ifelse(is.na(x), x, paste(round(x*100,1), "%", sep=""))),
                  "Taco Share: ",
                  sapply(states$tacos, function(x) ifelse(is.na(x), x, paste(round(x*100,1), "%", sep="")))) %>%
  lapply(htmltools::HTML)

```

Row {data-height=600}
-------------------------------------
  
### Market Share by State 
  
```{r}
```

### Pizza Market Growth by State
```{r}
# # dummy data for total sales by state i've lived in
df %>% filter(state %in% c('NY', 'SC', 'NC', 'DC', 'MI')) %>%
  mutate(pizza = pizza*runif(1, 100000, 1000000),
         tacos = tacos*runif(1, 100000, 1000000)) %>% 
  plot_ly(x = ~q, y = ~pizza, z = ~state, color=~state) %>%
  add_lines()
```

Row {.tabset .tabset-fade}
-------------------------------------

### Market Share Time Series

```{r} 

ggplotly(ggplot(df %>% group_by(q) %>%
                  summarise(pizza = mean(pizza),
                            tacos = mean(tacos)) %>%
                  gather(key = 'category', value = 'share', pizza:tacos)) +
           geom_line(aes(x=q, y=share, color=category)) + 
           theme_minimal() + theme(legend.title=element_blank()))

```

### Raw Data

```{r} 

datatable(head(df, 100) %>% 
           select(Quarter = q, State = state, Pizza_Share = pizza, Taco_Share = tacos),
          rownames = F,
          options = list(
            columnDefs = list(list(className = 'dt-center', targets = 0:3)),
            pageLength = 100,
            dom = 'ft')) %>%
   formatPercentage(c('Pizza_Share', 'Taco_Share'), 1)

```