【发布时间】:2019-12-09 22:28:31
【问题描述】:
我正在尝试使用闪亮和传单来根据数值变量(“人口”)的值打开/关闭点图上的点
我加载库和数据
#load libraries
library(shiny)
library(leaflet)
#create fake data
fake_data <- data.frame(Lat = c(51.4, 51.5, 51.7, 53.4, 50.7, 50.9, 51.1, 51.3, 51.4, 51.2),
Long = c(-0.1, -0.1, 0.0, -2.1, -2.4, -1.3, -0.3, -0.2, -0.3, 0.1),
Population = c(723, 746, 512, 389, 253, 289, 212, 208, 245, 212),
Class1 = c(TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE),
Class2 = c(TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE),
TownName = c("Town1", "Town2", "Town3", "Town4", "Town5", "Town6", "Town7", "Town8", "Town9", "Town10"))
并在 rStudio 中启动闪亮的应用程序,根据 fake.data$Population 的范围创建一个范围滑块
#shiny
##ui
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", "Population", min(fake_data$Population), max(fake_data$Population), value = range(fake_data$Population)
)
)
)
我认为问题出现在闪亮应用程序的服务器端代码块上 - 无论是在创建滑块数据时,还是在使用 observe() 函数时。
使用observe() 从sliderData 获取数据,如何根据fake_data$Class1 == TRUE 添加标记?
server <- function(input, output, session) {
sliderData <- reactive({
fake_data[fake_data$Population >= input$range[1] & fake_data$Population <= input$range[2],]
})
output$map <- renderLeaflet({
leaflet(fake_data) %>%
addProviderTiles(providers$Stamen.TonerLite, options = providerTileOptions(minZoom = 6, maxZoom = 10)) %>%
setView(lng=-1.7, lat=53.9, zoom=6)
})
observe({
leafletProxy("map", data = sliderData()) %>%
clearMarkers() %>%
addCircleMarkers(data = fake_data[fake_data$Class1 == TRUE,], group = "Class1", popup = ~as.character(TownName), color = 'black', fillOpacity = 1) %>%
addCircleMarkers(data = fake_data[fake_data$Class2 == TRUE,], group = "Class2", popup = ~as.character(TownName), color = 'red', fillOpacity = 1) %>%
addLayersControl(
overlayGroups = c("Class1", "Class2"),
options = layersControlOptions(collapsed = FALSE),
position = "bottomright"
)
})
我希望能够使用滑块设置人口范围,并让传单地图仅显示“人口”在此范围内的那些“城镇”的点。
【问题讨论】:
标签: r shiny leaflet gis r-leaflet