I am trying to create a network on R shiny with Leaflet. I have created the makrers on the map and some user controls (slider bars and drop downs). Now i need one last functionality (stuck for 2 days here).. I want to be able to click a marker and add some polylines based on which marker i clicked. See below my code and the data structure i am using.
Any help is greatly appreciated. I am extremely new to Shiny and stuck here for days !!
library(leaflet)
library(shiny)
library(sqldf)
setwd("C:/Users/sn10203/Desktop/Referral Network")
options(viewer = NULL)
newdata <- read.csv('C:/Users/sn10203/Desktop/Referral Network/map.csv')
air <- read.csv('C:/Users/sn10203/Desktop/Referral Network/details.csv')
pal <- colorFactor(palette = "Set1",domain = air$state)
ui <- fluidPage(
sliderInput(inputId = "cnt",
label = "Total Number: ",
min = 0, max = 1000, step =100, value = 2000),
selectInput(inputId = "aff",
label = "Affiliation Type:",
choices = list("ALL","ACADEMIC","COMMUNITY"),
selected = "ALL"),
leafletOutput("MapPlot1")
)
server <- function(input, output, session) {
output$MapPlot1 <- renderLeaflet({
leaflet(data=air) %>% setView(-98.35, 39.7,zoom = 4) %>%
addTiles() %>%
addCircleMarkers(~long,~LAT,popup = ~as.character(NAME),
label = ~as.character(CITY), color= ~pal(SPECDESC),
stroke = FALSE, radius = ~sqrt(sqrt(cnt))*1.3 ,fillOpacity = 1,
layerId = ~custid)
})
ranges <- reactiveValues(y = NULL)
observe({
cntinp <- input$cnt
affinp <- input$aff
clickinp <- input$map_marker_click
sites <- data.frame(subset(air, cnt <= cntinp))
if (input$aff != "ALL"){
sites <- data.frame(subset(air, cnt <= cntinp & type %in% affinp))
}
sites
if(!is.null(clickinp)){
subsetdata <- subset(newdata, custid == clickinp$id)
SelectedGroup <- subsetdata[!duplicated(subsetdata["group"]),]
ranges$y <- data.frame(subset(newdata, group %in% SelectedGroup$group))
}else {
ranges$y <- NULL
}
colorLevel <- sites$SPECDESC
popupLevel <- sites$NAME
labelLevel <- sites$CITY
radiusLevel <- sites$cnt
connectLon <- ranges$y$start_lon
connectLat <- ranges$y$start_lat
connectGroup <- ranges$y$group
if (is.null(ranges$y)){
leafletProxy("MapPlot1", session) %>% clearMarkers() %>%
addCircleMarkers(lng = sites$long,
lat = sites$LAT,
popup = as.character(popupLevel),
label = as.character(labelLevel),
radius = sqrt(sqrt(radiusLevel)),
fillOpacity = 1,
color= pal(colorLevel))
} else {
leafletProxy("MapPlot1", session) %>% clearMarkers() %>%
addCircleMarkers(lng = sites$long,
lat = sites$LAT,
popup = as.character(popupLevel),
label = as.character(labelLevel),
radius = sqrt(sqrt(radiusLevel)),
fillOpacity = 1,
color= pal(colorLevel)) %>%
addPolylines(lng = connectLon, lat = connectLat, group = connectGroup)
}
})
}
shinyApp(ui, server)
Data Structure
Map:
custid start_lat start_lon cnt src group
58297319 42.120563 -72.604468 1 FROM A1
55077692 46.688952 -67.991914 3 FROM A10
57934596 25.98821 -80.281374 2 FROM A100
56430004 41.54786 -72.089488 1 FROM A1000
58571603 36.549613 -82.09404 2 FROM A10000
59107036 42.364061 -72.458739 1 TO A1
56043247 42.134009 -72.565378 3 TO A10
57000734 42.337105 -71.105696 2 TO A100
56784860 41.293934 -72.932028 1 TO A1000
55958343 36.52686 -82.571968 2 TO A10000
details:
custid CUST_ID NAME CITY ZIP SPECDESC LAT long cnt type
58297319 58297319 AA AMHERST 1002 RADIOLOGY - RADIATION ONCOLOGY 42.364061 -72.458739 944 ACADEMIC
55077692 55077692 AA SPRINGFIELD 1104 INTERNAL MEDICINE - HEMATOLOGY & ONCOLOGY 42.134009 -72.565378 420 ACADEMIC
59107036 59107036 AA SPRINGFIELD 1199 INTERNAL MEDICINE - HEMATOLOGY 42.120563 -72.604468 32 ACADEMIC
57934596 57934596 AA BOSTON 2115 THORACIC SURGERY (CARDIOTHORACIC VASCULAR SURGERY) 42.337105 -71.105696 650 COMMUNITY
56043247 56043247 AA PRESQUE ISLE 4769 PHYSICIAN ASSISTANT 46.688952 -67.991914 266 ACADEMIC
56784860 56784860 AA NORWICH 6360 INTERNAL MEDICINE - HEMATOLOGY 41.54786 -72.089488 308 COMMUNITY
56430004 56430004 AA NEW HAVEN 6519 RADIOLOGY - RADIATION ONCOLOGY 41.293934 -72.932028 162 ACADEMIC
57000734 57000734 AA MIRAMAR 33025 THORACIC SURGERY (CARDIOTHORACIC VASCULAR SURGERY) 25.98821 -80.281374 204 COMMUNITY
55958343 55958343 AA BRISTOL 37620 RADIOLOGY - THERAPEUTIC RADIOLOGY 36.549613 -82.09404 339 ACADEMIC
58571603 58571603 AA KINGSPORT 37660 INTERNAL MEDICINE 36.52686 -82.571968 149 ACADEMIC