R shiny checkboxGroup 在地图上绘制数据
R shiny checkboxGroup to plot data on map
我对闪亮很陌生,我有一个问题。
我有一个简单的数据集,其中包含在特定位置 (X,Y) 的物种 (Species) 的观测值 (Number_Total)。
我想生成一张地图,使您能够 select 下拉菜单中的物种。 Shiny 然后会显示您是地图上出现的物种。
我已经走得很远了(根据我的经验),但是 select 在菜单中输入物种没有任何作用...
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)
我知道我的代码可能很乱,但我还是要归咎于我的经验 =)
我没能马上在这里得到一个示例数据集,所以这里是图片
这是上面代码的结果(数据略有不同)
enter image description here
这是您需要的。我认为您的技能足以理解这一点,但如果您有任何问题,请发表评论。
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)
})
}
我对闪亮很陌生,我有一个问题。
我有一个简单的数据集,其中包含在特定位置 (X,Y) 的物种 (Species) 的观测值 (Number_Total)。
我想生成一张地图,使您能够 select 下拉菜单中的物种。 Shiny 然后会显示您是地图上出现的物种。
我已经走得很远了(根据我的经验),但是 select 在菜单中输入物种没有任何作用...
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)
我知道我的代码可能很乱,但我还是要归咎于我的经验 =)
我没能马上在这里得到一个示例数据集,所以这里是图片
这是上面代码的结果(数据略有不同) enter image description here
这是您需要的。我认为您的技能足以理解这一点,但如果您有任何问题,请发表评论。
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)
})
}