library(mapview)
library(aqm)
## 
## Attaching package: 'aqm'
## The following object is masked from 'package:stats':
## 
##     dt
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.1.2, PROJ 4.9.3
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data(traffic)


# traffic$Date<-gsub("30-Sep","2019-09-30",traffic$Date)
# traffic$Date<-gsub("03-Oct","2019-10-03",traffic$Date)
# traffic$Date<-lubridate::ymd(traffic$Date)
# save(traffic, file ="traffic.rda")
traffic %>% group_by(Id,Direction, Date) %>% summarise(Count=sum(Count)) -> totals
map<-mapview(totals,alpha.regions=0,alpha=0)
cds<-st_coordinates(totals)
totals %>% st_set_geometry(NULL) -> totals
totals<-data.frame(totals,cds)
totals$Direction<-as.factor(totals$Direction)
str(totals)
## 'data.frame':    24 obs. of  6 variables:
##  $ Id       : int  1 1 1 1 2 2 2 2 3 3 ...
##  $ Direction: Factor w/ 4 levels "E","N","S","W": 1 1 4 4 1 1 4 4 2 2 ...
##  $ Date     : Date, format: "2019-09-30" "2019-10-03" ...
##  $ Count    : int  2874 2825 2556 1998 2380 2819 1945 1757 2234 1855 ...
##  $ X        : num  -1.9 -1.9 -1.9 -1.9 -1.89 ...
##  $ Y        : num  50.7 50.7 50.7 50.7 50.7 ...
library(tidyr)
library(dplyr)

totals %>% pivot_wider(names_from = Direction,values_from = Count, values_fill = list(Count=0)) -> totals
library(sf)
library(mapview)


library(leaflet.minicharts)
library(leaflet.extras)
## Loading required package: leaflet
map@map %>%
  addMinicharts(
    totals$X,totals$Y, 
    chartdata = totals[,5:8],
    width=100,
    height=100,
    time = totals$Date
   
  )  %>% addFullscreenControl()