以下示例取自传单上的RStudio教程.我稍微修改了它以适合我的问题.
我有一张地图(这里是地震),我在地图上使用它addCircleMarkers,当点击时,弹出窗口会显示一些信息.我想在真正的应用程序中做的是,当在地图上单击标记时,它会将页面上的其他图形过滤为仅与该标记相关的数据.我知道如何获取用户点击使用位置的信息input$map_marker_click- 这将为我提供足够满足我需求的纬度和经度.但是 - 一旦设置,此值不会更改.NULL当用户在非标记区域中单击地图时,它不会还原.如何检测用户是否在标记以外的其他位置单击了地图并重置input$map_marker_click为NULL
下面的例子没有其他图表,但我确实显示了它的值 input$map_marker_click
library(shiny)
library(leaflet)
library(RColorBrewer)
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width = "100%", height = "100%"),
absolutePanel(top = 10, right = 10,
sliderInput("range", "Magnitudes", min(quakes$mag), max(quakes$mag),
value = range(quakes$mag), step = 0.1
),
selectInput("colors", "Color Scheme",
rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
),
checkboxInput("legend", "Show legend", TRUE),
verbatimTextOutput("clickInfo")
)
)
server <- function(input, output, session) {
output$clickInfo = renderPrint({input$map_marker_click})
filteredData <- reactive({
quakes[quakes$mag >= input$range[1] & quakes$mag <= input$range[2],]
})
colorpal <- reactive({
colorNumeric(input$colors, quakes$mag)
})
output$map <- renderLeaflet({
leaflet(quakes) %>% addTiles() %>%
fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat))
})
observe({
pal <- colorpal()
leafletProxy("map", data = filteredData()) %>%
clearShapes() %>%
addCircleMarkers(radius = ~mag^2/3, weight = 1, color = "#777777",
fillColor = ~pal(mag), fillOpacity = 0.7, popup = ~paste(mag)
)
})
observe({
proxy <- leafletProxy("map", data = quakes)
proxy %>% clearControls()
if (input$legend) {
pal <- colorpal()
proxy %>% addLegend(position = "bottomright",
pal = pal, values = ~mag
)
}
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
我在这里问了同样的问题,用户NicE在那里提供了一个解决方案.
如果有人遇到此页面寻找解决方案,下面的代码实现了上述请求,当在标记之后单击地图时,将点击值重置为NULL.示例中唯一的附加代码是两行#s之间.
library(shiny)
library(leaflet)
library(RColorBrewer)
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width = "100%", height = "100%"),
absolutePanel(top = 10, right = 10,
sliderInput("range", "Magnitudes", min(quakes$mag), max(quakes$mag),
value = range(quakes$mag), step = 0.1
),
selectInput("colors", "Color Scheme",
rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
),
checkboxInput("legend", "Show legend", TRUE),
verbatimTextOutput("clickInfo")
)
)
server <- function(input, output, session) {
#########################################################
data <- reactiveValues(clickedMarker=NULL)
observeEvent(input$map_marker_click,
{data$clickedMarker <- input$map_marker_click})
observeEvent(input$map_click,
{data$clickedMarker <- NULL})
output$clickInfo <- renderPrint({data$clickedMarker})
##########################################################
filteredData <- reactive({
quakes[quakes$mag >= input$range[1] & quakes$mag <= input$range[2],]
})
colorpal <- reactive({
colorNumeric(input$colors, quakes$mag)
})
output$map <- renderLeaflet({
leaflet(quakes) %>% addTiles() %>%
fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat))
})
observe({
pal <- colorpal()
leafletProxy("map", data = filteredData()) %>%
clearShapes() %>%
addCircleMarkers(radius = ~mag^2/3, weight = 1, color = "#777777",
fillColor = ~pal(mag), fillOpacity = 0.7, popup = ~paste(mag)
)
})
observe({
proxy <- leafletProxy("map", data = quakes)
proxy %>% clearControls()
if (input$legend) {
pal <- colorpal()
proxy %>% addLegend(position = "bottomright",
pal = pal, values = ~mag
)
}
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)