---
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}
```
Row
-------------------------------------
### Market Share Time Series
```{r}
```
### 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)
```