【发布时间】:2016-10-23 20:11:00
【问题描述】:
请注意,此问题已发布at the R Shiny Google Group:
在Leaflet for R/ Shiny Integration documentation 之后,leafletProxy 函数出现了不需要/意外的行为。
在下面的应用程序中,我希望圆圈标记会随着 input$choices 的变化而出现/消失。
反应式 df 'filteredData' 似乎工作正常。
我是否错误地使用了 leafletProxy() 或 clearShapes()?
library(shiny)
library(dplyr)
library(leaflet)
my_df <- data.frame(lat = 34.72 + rnorm(1000, sd = .18),
lng = -92.5 + rnorm(1000, sd = .33),
category = c(rep("A", 300), rep("B", 300), rep("C", 400)))
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", height = '100%', width = '100%'),
absolutePanel(top = 10, right = 10,
checkboxGroupInput("choices", "Choices", choices = list("A","B","C"), selected = c("A","B","C")),
verbatimTextOutput("my_rows")
)
)
server <- function(input, output) {
filteredData <- reactive( my_df %>% filter(category %in% input$choices) )
output$map <- renderLeaflet({ leaflet() %>% addTiles() %>% setView(lat = 34.72, lng = -92.5, zoom = 9) })
observe({
leafletProxy("map", data = filteredData()) %>% clearShapes() %>% addCircleMarkers(radius = 6, weight = 1, fillColor = "red", fillOpacity = 0.3)
})
output$my_rows <- renderPrint({ filteredData() %>% nrow() })
}
shinyApp(ui = ui, server = server)
【问题讨论】: