[英]R shiny checkboxGroup to plot data on map
我是新手,我有一个问题。
我有一个简单的数据集,它在某个位置(X,Y)中具有对物种(Species)的观察(Number_Total)。
我想生成一张地图,使您能够在下拉菜单中选择物种。 然后,Shiny会显示您是该物种出现在地图上。
我走得很远(以我的经验为准),但是在菜单中选择物种并没有任何作用...
ui <- (fluidPage(titlePanel("Species Checker"),
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE)
),
mainPanel(
leafletOutput("CountryMap",
width = 1000, height = 500))
)
))
服务器端
server <- function(input, output, session){
output$CountryMap <- renderLeaflet({
leaflet() %>% addTiles() %>%
setView(lng = 10, lat = 40, zoom = 5) %>%
addCircles(lng = df$Y, lat = df$X, weight = 10,
radius =sqrt(df$Number_Total)*15000, popup = df$Species)
})
observeEvent(input$species, {
if(input$species != "")
{
leafletProxy("CountryMap") %>% clearShapes()
index = which(df$Species == input$species)
leafletProxy("CountryMap")%>% addCircles(lng = df$X[index],
lat = df$Y[index],
weight = 1,
radius =sqrt(df$Number_Total[index])*30, popup = df$Species[index])
}
})
}
最后画出来
shinyApp(ui = ui, server = server)
我知道我的代码可能很凌乱,但我还是怪我的经验=)我没有设法立即在此处获取示例数据集,因此此处以图片的形式出现
这是上述代码的结果(数据略有不同),请在此处输入图片说明
这就是你所需要的。 我认为您有足够的技巧来理解这一点,但是如果您有任何疑问,请发表评论。
server <- function(input, output, session) {
# map_data <- reactive({
# req(input$species)
# df[df$Species %in% input$species, ]
# })
output$CountryMap <- renderLeaflet({
leaflet() %>% addTiles() %>%
setView(lng = 10, lat = 40, zoom = 5)
})
map_proxy <- leafletProxy("CountryMap")
observe({
md <- df[df$Species %in% input$species, ]
map_proxy %>%
addCircles(lng = md$Y, lat = md$X, weight = 10,
radius = sqrt(md$Number_Total)*15000, popup = md$Species)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.