Second time using leaflet and a little confused between the interaction of reactive filter data and observe.
I have checkbox and selectInput options which filter parts of database to produce a map with long/lat entries. The reactive portion does not pull the necessary filtered data. Hoping to get some direction.
peo <- read.csv("https://raw.githubusercontent.com/cwong79/DATA608/master/PEO1.csv")
peo$NAICSClassCode <- as.numeric(substr(peo$NAICS, start = 1, stop = 2))
type <- c("Agriculture, Forestry, Fishing and Hunting",
"Mining",
"Utilities",
"Construction",
"Manufacturing",
"Wholesale Trade",
"Retail Trade",
"Transportation and Warehousing",
"Information",
"Finance and Insurance",
"Real Estate Rental and Leasing",
"Professional, Scientific, and Technical Services",
"Management of Companies and Enterprises",
"Administrative and Support and Waste Management and Remediation Services",
"Educational Services",
"Health Care and Social Assistance",
"Arts, Entertainment, and Recreation",
"Accommodation and Food Services",
"Other Services (except Public Administration)",
"Public Administration")
naicsdata <- data.frame(type)
peo$NAICS_TYPE <- cut(peo$NAICSClassCode, c(1, 11, 21, 22, 23, 34, 42, 46, 50, 51, 52, 53, 54, 55, 57, 61, 63, 71, 73, 82, Inf), type)
peo$NAICS_TYPE <- as.character(peo$NAICS_TYPE)
peo$NAICS_TYPE[is.na(peo$NAICS_TYPE)] <- "Unknown"
peo$LOCATION.EFFECTIVE.DATE <- as.Date(peo$LOCATION.EFFECTIVE.DATE, "%m/%d/%y")
peo$RenewalMonth <- format(peo$LOCATION.EFFECTIVE.DATE, "%B")
tier1 <- c("ADP TOTAL SOURCE INC", "A 1 HR A DIVISION OF OASIS OUTSOURCING INC", "COADVANTAGE CORP", "INSPERITY INC", "OASIS ACQUISITION INC", "OASIS ACQUISITION INC A PAYCHEX CO", "OASIS DHR LLC", "OASIS OUTSOURCING CONTRACT II INC", "OASIS OUTSOURCING INC", "PAYCHEX BUSINESS SOLUTIONS LLC", "PAYCHEX HR OUTSOURCING LLC", "TRINET GROUP INC", "TRINET HR II HOLDINGS INC", "TRINET HR IV LLC")
tier2 <- c("ALLY HR LLC DBA MATRIXONESOURCE", "ALPHASTAFF GROUP INC", "CHOICE EMPLOYER SOLUTIONS INC", "CORNERSTONE CAPITAL GROUP INC", "DECISION HR", "FLORIDA RESOURCE MANAGEMENT LLC", "FRANKCRUM 2 INC", "IMPACT STAFF LEASING LLC", "JUSTWORKS EMPLOYMENT GROUP LLC", "KYMBERLY GROUP PAYROLL SOLUTIONS INC", "OCMI III INC DBA PEOPAYGO", "REGIS GROUP HOLDINGS INC", "SOUTH EAST PERSONNEL LEASING INC", "STAFFLINK OUTSOURCING INC", "THE S2 HR GROUP LLC", "TLR OF BONITA INC", "WORKFORCE BUSINESS SERVICES INC")
peo$Tier <- with(peo, ifelse(NAMED.INSURED %in% tier1, "1",
ifelse(NAMED.INSURED %in% tier2, "2", "3")))
#Set up ui
ui <- fluidPage(
titlePanel("PEO Choices"),
sidebarPanel(h5("", width=1),
checkboxGroupInput(inputId = "TierFlag",
label = h4("Tier"),
choices = setNames(object = c("1", "2", "3"),
nm = c("1", "2", "3")),
selected = c("1", "2", "3")),
selectInput(inputId = "PEOType",
label = h4("PEO"),
choices = sort(unique(peo$NAMED.INSURED)),
multiple = TRUE),
sliderInput(inputId = "month",
label = h4("Month"),
min = 1,
max = 12,
value = c(2, 10),
step = 1,
width = "100%",
ticks = FALSE),
position="right"),
#App mainPanel content and styles
mainPanel(fluidRow(leafletOutput(outputId = "map")))
)
#Set up server
server <- function(input, output){
#Build leaflet map
#Set colors manually
pal <- colorFactor(
palette = c('#b2df8a', '#1f78b4', '#feb24c'),
domain = peo$Tier
)
#Build leaflet map
map <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
setView(-80.121, 26.194, zoom = 9) %>%
addProviderTiles("MapBox",
options = providerTileOptions(id = "mapbox.light", noWrap = FALSE,
accessToken = 'pk.eyJ1IjoiY3dvbmc3OSIsImEiOiJjazNkNW4wOTQwa3pjM2Jva3JwZHB0OXFmIn0.h-12OxqTpTI0Pj7Wk7HJnQ')) %>%
addCircles(lng = peo$Longitude, lat = peo$Latitude,
popup = paste("Company Name:", peo$EMPLOYER, "<br>",
"PEO Type:", peo$NAMED.INSURED, "<br>",
"Industry:", peo$NAICS_TYPE, "<br>"),
weight = 2, opacity = 0.5, radius = 5,
color = pal(peo$Tier),
group = "myMarkers") %>%
addLegend('bottomright',
title = "Tiers",
pal = pal,
values = peo$Tier,
opacity = 1)
#Filter data
datFilt <- reactive({
PeoSearch <- paste0(c('x', input$TierFlag), collapse = "|")
PeoSearch <- gsub(",","|", PeoSearch)
peo[grepl(PeoSearch, peo$Tier) & peo$NAMED.INSURED %in% input$PEOType]
})
observe({
if(nrow(datFilt())==0) {print("Nothing selected");leafletProxy("map") %>% clearShapes()}
else{ #print(paste0("Selected: ", unique(input$TierFlag)))
leafletProxy("map", data=datFilt()) %>%
clearShapes() %>%
addCircles(lng = datFilt()$Longitude, lat = datFilt()$Latitude,
popup = paste("Company Name:", datFilt()$EMPLOYER, "<br>",
"PEO Type:", datFilt()$NAMED.INSURED, "<br>",
"Industry:", datFilt()$NAICS_TYPE, "<br>"),
weight = 2, opacity = 0.5, radius = 5,
color = pal(datFilt()$Tier)) %>%
addLegend('bottomright',
title = "Tiers",
pal = pal,
values = peo$Tier,
opacity = 1)
}
})
output$map <- renderLeaflet(map)
}
#Run app
shinyApp(ui = ui, server = server)