我正在使用响应函数在R Shiny中创建一个传单地图。
我通过为每个标记创建不同的图标来格式化不同类型的标记,然后在传单地图输出中使用“ icon = booked_o_icon”(等等)进行指定。结果,对于每种图标类型,我都有一个不同的“ addMarkers”部分。
## app.R ##
library(shiny)
library(shinydashboard)
library(leaflet)
not_booked <- rides[which(rides$status=="Not booked"),]
unbooked <- rides[which(rides$status=="Unbooked"),]
booked <- rides[which(rides$status=="Booked"),]
ui <- dashboardPage(
dashboardHeader(title = "Rides"),
dashboardSidebar(),
dashboardBody(
fluidRow(hr(),
dateRangeInput <- dateRangeInput(
inputId = "daterange",
label = "Select the date range",
start = min("2018-04-25"),
end = max("2018-11-23"),
min = min("2018-04-25"),
max = max("2018-11-23"),
format = "yyyy-mm-dd",
separator = "-"
),
textOutput("range"),
tableOutput("subdata"),
leafletOutput("leafletMap")
))
)
# Format markers
booked_o_icon <- makeIcon(
"/home/kimberly/Downloads/origin_booked.svg",
iconWidth = 32, iconHeight = 32)
booked_d_icon <- makeIcon(
"/home/kimberly/Downloads/destination_booked.svg",
iconWidth = 32, iconHeight = 32)
unbooked_o_icon <- makeIcon(
"/home/kimberly/Downloads/origin_unbooked.svg",
iconWidth = 32, iconHeight = 32)
unbooked_d_icon <- makeIcon(
"/home/kimberly/Downloads/destination_unbooked.svg",
iconWidth = 32, iconHeight = 32)
not_booked_o_icon <- makeIcon(
"/home/kimberly/Downloads/origin_notbooked.svg",
iconWidth = 32, iconHeight = 32)
not_booked_d_icon <- makeIcon(
"/home/kimberly/Downloads/destination_notbooked.svg",
iconWidth = 32, iconHeight = 32)
server <- function(input, output, session) {
output$subdata <- renderTable({
s = subset(rides, rides$date>=input$daterange[1] & rides$date<= input$daterange[2])
table(s$status)
})
# Reactive function
reactiveData <- reactive({
rides[rides$date>=input$daterange[1] & rides$date<= input$daterange[2],]
})
output$leafletMap <- renderLeaflet({
leaflet(data = reactiveData()) %>%
addProviderTiles("OpenStreetMap") %>%
addMarkers(data=booked,
lat = booked$origin_lat,
lng = booked$origin_lon,
group = "Booked",
popup = as.character.Date(booked$date),
clusterOptions = markerClusterOptions(),
icon = booked_o_icon) %>%
addMarkers(data=not_booked,
lat = not_booked$origin_lat,
lng = not_booked$origin_lon,
popup = as.character.Date(not_booked$date),
clusterOptions = markerClusterOptions(),
icon = not_booked_o_icon) %>%
addMarkers(data=unbooked,
lat = unbooked$origin_lat,
lng = unbooked$origin_lon,
popup = as.character.Date(unbooked$date),
clusterOptions = markerClusterOptions(),
icon = unbooked_o_icon) %>%
addMarkers(data=booked,
lat = booked$dest_lat,
lng = booked$dest_lon,
popup = as.character.Date(booked$date),
clusterOptions = markerClusterOptions(),
icon = booked_d_icon) %>%
addMarkers(data=not_booked,
lat = not_booked$dest_lat,
lng = not_booked$dest_lon,
popup = as.character.Date(not_booked$date),
clusterOptions = markerClusterOptions(),
icon = not_booked_d_icon) %>%
addMarkers(data=unbooked,
lat = unbooked$dest_lat,
lng = unbooked$dest_lon,
popup = as.character.Date(unbooked$date),
clusterOptions = markerClusterOptions(),
icon = unbooked_d_icon)
})
# Observe function
observeEvent(input$dateRangeInput, {leafletProxy("leafletMap", data=reactiveData()) %>%
clearMarkers(input$dateRangeInput)
})
}
shinyApp(ui, server)
当我对标记进行聚类时,它最终会聚类地图的所有类型的标记(共有6种)。如何使所有点聚集在一起,但保持不同的图标?