Libraries

library(readxl)
#import excel data set 
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
library(tidyr)
library(stringr)
library(ggplot2)
library(knitr)
library(magrittr)
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:tidyr':
## 
##     extract
library(tmap)
library(tmaptools)
library(leaflet)
# creates maps
library(readr)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ tibble  2.1.1     ✔ purrr   0.3.2
## ✔ tibble  2.1.1     ✔ forcats 0.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ magrittr::extract() masks tidyr::extract()
## ✖ dplyr::filter()     masks stats::filter()
## ✖ dplyr::lag()        masks stats::lag()
## ✖ purrr::set_names()  masks magrittr::set_names()
library(tigris)
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
## 
## Attaching package: 'tigris'
## The following object is masked from 'package:graphics':
## 
##     plot
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(ggrepel)
library(ggthemes)
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.3-4, (SVN revision 766)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.1.3, released 2017/20/01
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rgdal/gdal
##  GDAL binary built with GEOS: FALSE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rgdal/proj
##  Linking to sp version: 1.3-1
library(caret)
## Loading required package: lattice
## 
## Attaching package: 'caret'
## The following object is masked from 'package:purrr':
## 
##     lift
library(forcats)
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
library(extrafont)
## Registering fonts with R
library(plyr)
## -------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## -------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following object is masked from 'package:purrr':
## 
##     compact
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
library(choroplethr)
## Loading required package: acs
## Loading required package: XML
## 
## Attaching package: 'acs'
## The following object is masked from 'package:dplyr':
## 
##     combine
## The following object is masked from 'package:base':
## 
##     apply
library(choroplethrMaps)
library(formattable)
## 
## Attaching package: 'formattable'
## The following objects are masked from 'package:scales':
## 
##     comma, percent, scientific
library(mapview)
## 
## Attaching package: 'mapview'
## The following object is masked from 'package:leaflet':
## 
##     addMapPane
library(rsconnect)

Load Data

Cannabis_Licenses <- read_excel("Cannabis_Location_Dataset.xlsx")
Cannabis_Licenses
Cannabis_Licenses$Long <- as.numeric(Cannabis_Licenses$Long)
Cannabis_Licenses$Lat <- as.numeric(Cannabis_Licenses$Lat)

# Convert to Percent 
percent <- function(x, digits = 2, format = "f", ...) {
  paste0(formatC(100 * x, format = format, digits = digits, ...), "%")
}

Cannabis_Licenses$White_Authority <- percent(Cannabis_Licenses$White_Authority)
Cannabis_Licenses$Native_Authority <- percent(Cannabis_Licenses$Native_Authority)
Cannabis_Licenses$Asian_Authority <- percent(Cannabis_Licenses$Asian_Authority)
Cannabis_Licenses$Black_Authority <- percent(Cannabis_Licenses$Black_Authority)
Cannabis_Licenses$No_Answer_Authority <- percent(Cannabis_Licenses$No_Answer_Authority)
Cannabis_Licenses$Latinx_Authority <- percent(Cannabis_Licenses$Latinx_Authority)
Cannabis_Licenses$Middle_Eastern_Authority <- percent(Cannabis_Licenses$Middle_Eastern_Authority)
Cannabis_Licenses$Other_Race_Authority <- percent(Cannabis_Licenses$Other_Race_Authority)

DBE

Cannabis_LicensesDBE <-Cannabis_Licenses %>%
  filter(DBE != "Not a DBE")
Cannabis_LicensesDBE
popup_dcrDBE <- paste0("<b>", Cannabis_LicensesDBE$Business_Name,
                    "</b><br />Total Licenses: ", "<b>",Cannabis_LicensesDBE$Total_Licenses,"</b>",
                    "</b><br />DBE: ", "<b>",Cannabis_LicensesDBE$DBE,"</b>",
                    "</b><br />Board Member: ", "<b>", Cannabis_LicensesDBE$`Board member`,"</b>",
                   "</b><br />Director: ", "<b>",Cannabis_LicensesDBE$Director,"</b>",
                    "</b><br />Employee: ", "<b>",Cannabis_LicensesDBE$Employee,"</b>",
                    "</b><br />Executive: ", "<b>",Cannabis_LicensesDBE$Executive,"</b>",
                    "</b><br />Manger: ", "<b>",Cannabis_LicensesDBE$Manager,"</b>",
                    "</b><br />Volunteer: ", "<b>",Cannabis_LicensesDBE$Volunteer,"</b>")

NumberDBE <- as.numeric(Cannabis_LicensesDBE$Total_Licenses)

cofDBE <- colorFactor(c("purple", "blue", "green", "pink", "orange"), domain=c("LGBTQI-Owned", "Woman-Owned", "Minority-Owned", "Veteran-Owned", "Disability-Owned"))

Cannabis_DBE_Map <- leaflet(Cannabis_LicensesDBE) %>% 
  addProviderTiles(providers$Esri.WorldGrayCanvas) %>% 
  setView(-71.931180, 42.385453, zoom = 8) %>% 
  addCircleMarkers(~Long, ~Lat, popup = ~popup_dcrDBE, weight = 3, radius = NumberDBE*4, 
             color= ~cofDBE(Cannabis_LicensesDBE$DBE), stroke = FALSE, fillOpacity = 0.7) %>% 
  addLegend("bottomright", colors= c("purple", "blue", "green", "pink", "orange", "red"),labels=c("Not a DBE", "LGBTQI-Owned", "Woman-Owned", "Minority-Owned", "Veteran-Owned", "Disability-Owned"), title="MA Cannabis DBE")

# Not DBE
Cannabis_Licenses_NotDBE <-Cannabis_Licenses %>%
  filter(DBE == "Not a DBE")

popup_dcrNotDBE <- paste0("<b>", Cannabis_Licenses_NotDBE$Business_Name,
                    "</b><br />Total Licenses: ", "<b>",Cannabis_Licenses_NotDBE$Total_Licenses,"</b>",
                    "</b><br />DBE: ", "<b>",Cannabis_Licenses_NotDBE$DBE,"</b>",
                    "</b><br />Board Member: ", "<b>", Cannabis_Licenses_NotDBE$`Board member`,"</b>",
                   "</b><br />Director: ", "<b>",Cannabis_Licenses_NotDBE$Director,"</b>",
                    "</b><br />Employee: ", "<b>",Cannabis_Licenses_NotDBE$Employee,"</b>",
                    "</b><br />Executive: ", "<b>",Cannabis_Licenses_NotDBE$Executive,"</b>",
                    "</b><br />Manger: ", "<b>",Cannabis_Licenses_NotDBE$Manager,"</b>",
                    "</b><br />Volunteer: ", "<b>",Cannabis_Licenses_NotDBE$Volunteer,"</b>")

NumberNotDBE <- as.numeric(Cannabis_Licenses_NotDBE$Total_Licenses)

cofNotDBE <- colorFactor(c("red"), domain=c("Not a DBE"))

Cannabis_NotDBE_Map <- leaflet(Cannabis_Licenses_NotDBE) %>% 
  addProviderTiles(providers$Esri.WorldGrayCanvas) %>% 
  setView(-71.931180, 42.385453, zoom = 8) %>% 
  addCircleMarkers(~Long, ~Lat, popup = ~popup_dcrNotDBE, weight = 3, radius = NumberNotDBE*2, 
             color= ~cofNotDBE(Cannabis_Licenses_NotDBE$DBE), stroke = FALSE, fillOpacity = 0.7) %>% 
  addLegend("bottomright", colors= c("red"),labels=c("Not a DBE"), title="MA Cannabis DBE")

sync(Cannabis_DBE_Map,Cannabis_NotDBE_Map)