如何使用 Shiny 在 R 中基于条件面板创建地图?
How to create maps based on conditional panel in R with Shiny?
我正在尝试根据用户 select 的内容创建几张传单地图。我试图制作一个可重现的例子。
我有一个有四种形状的 sf(即代表北卡罗来纳州的四个县)。用户可以根据县的选择(通过“checkboxGroupInput”)选择绘制一到四张地图。如果用户select一个县,我应该绘制一张代表该县的地图用户selected。如果用户 selected 两个县,则应绘制两张地图等
我有几行代码,但无法将用户 selection 连接到我的地图。您可以在下面找到我尝试做的事情。感谢您的帮助。
library(sf)
library(leaflet)
library(mapview)
library(mapedit)
library(DT)
library(viridis)
library(leafem)
nc <- st_read(system.file("shape/nc.shp", package="sf")) %>% head(3)
ui = fluidPage(
titlePanel("Maps"),
sidebarPanel(
checkboxGroupInput('MapNumber','Select Maps', choices = c("Ashe", "Alleghany", "Surry"), selected = "Ashe"),
conditionalPanel(condition="output.MapNum=='1'",
fluidRow(
column(12,
leafletOutput("Map1a")
)
)
),
conditionalPanel(condition="output.MapNum=='2'",
fluidRow(
column(6,
leafletOutput("Map2a")
),
column(6,
leafletOutput("Map2b")
)
)
),
conditionalPanel(condition="output.MapNum=='3'",
fluidRow(
column(6,
leafletOutput("Map3a")
),
column(6,
leafletOutput("Map3b")
),
column(6,
leafletOutput("Map3c")
)
)
)
)
)
server = function(input, output) {
MapInput1a<-reactive({
req(length(input$MapNumber)==1)
input$MapNumber[1]
})
MapInput2a<-reactive({
req(length(input$MapNumber)==2)
input$MapNumber[1]
})
MapInput2b<-reactive({
req(length(input$MapNumber)==2)
input$MapNumber[2]
})
MapInput3a<-reactive({
req(length(input$MapNumber)==3)
input$MapNumber[1]
})
MapInput3b<-reactive({
req(length(input$MapNumber)==3)
input$MapNumber[2]
})
MapInput3c<-reactive({
req(length(input$MapNumber)==3)
input$MapNumber[3]
})
output$Map1a<-renderLeaflet({leaflet()%>% addTiles(group = "OSM") %>%
addProviderTiles("CartoDB", group = "CartoDB") %>%
addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
addFeatures("?")})# do not know how to plot the feature correctly, and I do not know how to do for output$Map2a,
# output$Map2b, etc.
}
shinyApp(ui, server)
有趣的问题。我会使用一种完全不同的方法来完成工作。我不会创建那么多条件面板,而是编写一个函数,根据所选县的数量创建列集合。
library(sf)
library(leaflet)
library(shiny)
nc <- st_read(system.file("shape/nc.shp", package="sf")) %>% head(3)
# This function returns a collection of leaflet plots based on county names
maps_ui <-function(counties, shapefiles) {
n_counties <- length(counties)
colum_with <- 12 / n_counties
maps <- purrr::map(
counties,
~shiny::column(
width = colum_with,
leaflet() %>%
addTiles(group = "OSM") %>%
addProviderTiles("CartoDB", group = "CartoDB") %>%
addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
addPolygons(data = nc[nc$NAME == .x, ]) %>%
addLegend(
position = 'bottomright',
colors = c("#5663E2", "#F4BC35", "#E45F71"),
labels = c ("1", "2", "3"),
opacity = 0.5, title = .x )
)
)
)
return(maps)
}
# The app
ui = fluidPage(
checkboxGroupInput('counties', 'Select Maps', choices = c("Ashe", "Alleghany", "Surry"), selected = "Ashe"),
fluidRow(uiOutput('maps'))
)
server = function(input, output) {
output$maps <- renderUI({
maps_ui(input$counties, nc)
})
}
shinyApp(ui, server)
我正在尝试根据用户 select 的内容创建几张传单地图。我试图制作一个可重现的例子。 我有一个有四种形状的 sf(即代表北卡罗来纳州的四个县)。用户可以根据县的选择(通过“checkboxGroupInput”)选择绘制一到四张地图。如果用户select一个县,我应该绘制一张代表该县的地图用户selected。如果用户 selected 两个县,则应绘制两张地图等
我有几行代码,但无法将用户 selection 连接到我的地图。您可以在下面找到我尝试做的事情。感谢您的帮助。
library(sf)
library(leaflet)
library(mapview)
library(mapedit)
library(DT)
library(viridis)
library(leafem)
nc <- st_read(system.file("shape/nc.shp", package="sf")) %>% head(3)
ui = fluidPage(
titlePanel("Maps"),
sidebarPanel(
checkboxGroupInput('MapNumber','Select Maps', choices = c("Ashe", "Alleghany", "Surry"), selected = "Ashe"),
conditionalPanel(condition="output.MapNum=='1'",
fluidRow(
column(12,
leafletOutput("Map1a")
)
)
),
conditionalPanel(condition="output.MapNum=='2'",
fluidRow(
column(6,
leafletOutput("Map2a")
),
column(6,
leafletOutput("Map2b")
)
)
),
conditionalPanel(condition="output.MapNum=='3'",
fluidRow(
column(6,
leafletOutput("Map3a")
),
column(6,
leafletOutput("Map3b")
),
column(6,
leafletOutput("Map3c")
)
)
)
)
)
server = function(input, output) {
MapInput1a<-reactive({
req(length(input$MapNumber)==1)
input$MapNumber[1]
})
MapInput2a<-reactive({
req(length(input$MapNumber)==2)
input$MapNumber[1]
})
MapInput2b<-reactive({
req(length(input$MapNumber)==2)
input$MapNumber[2]
})
MapInput3a<-reactive({
req(length(input$MapNumber)==3)
input$MapNumber[1]
})
MapInput3b<-reactive({
req(length(input$MapNumber)==3)
input$MapNumber[2]
})
MapInput3c<-reactive({
req(length(input$MapNumber)==3)
input$MapNumber[3]
})
output$Map1a<-renderLeaflet({leaflet()%>% addTiles(group = "OSM") %>%
addProviderTiles("CartoDB", group = "CartoDB") %>%
addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
addFeatures("?")})# do not know how to plot the feature correctly, and I do not know how to do for output$Map2a,
# output$Map2b, etc.
}
shinyApp(ui, server)
有趣的问题。我会使用一种完全不同的方法来完成工作。我不会创建那么多条件面板,而是编写一个函数,根据所选县的数量创建列集合。
library(sf)
library(leaflet)
library(shiny)
nc <- st_read(system.file("shape/nc.shp", package="sf")) %>% head(3)
# This function returns a collection of leaflet plots based on county names
maps_ui <-function(counties, shapefiles) {
n_counties <- length(counties)
colum_with <- 12 / n_counties
maps <- purrr::map(
counties,
~shiny::column(
width = colum_with,
leaflet() %>%
addTiles(group = "OSM") %>%
addProviderTiles("CartoDB", group = "CartoDB") %>%
addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
addPolygons(data = nc[nc$NAME == .x, ]) %>%
addLegend(
position = 'bottomright',
colors = c("#5663E2", "#F4BC35", "#E45F71"),
labels = c ("1", "2", "3"),
opacity = 0.5, title = .x )
)
)
)
return(maps)
}
# The app
ui = fluidPage(
checkboxGroupInput('counties', 'Select Maps', choices = c("Ashe", "Alleghany", "Surry"), selected = "Ashe"),
fluidRow(uiOutput('maps'))
)
server = function(input, output) {
output$maps <- renderUI({
maps_ui(input$counties, nc)
})
}
shinyApp(ui, server)