我正在try 根据用户 Select 的内容创建几张传单 map .我试图制作一个可复制的例子.

我得到了几行代码,但我无法将用户 Select 连接到我的 map .下面你可以找到我试图做的事情.谢谢你的帮助.

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)

enter image description here

enter image description here

R相关问答推荐

当还使用模型列表时,是否可以使用forest_mode包的面板设置?(R统计分析)

如何计算具有NA的行的更改百分比

通过R访问MoveApps API

R通过字符串中的索引连接数据帧r

按自定义数字模式对变量名称排序

在ComplexHeatmap中,如何更改anno_barplot()标题的Angular ?

使用spatVector裁剪网格数据时出现的问题

咕噜中的元素列表:map

警告:lmdif:info = 0. nls. lm()函数的输入参数不正确

R Sapply函数产生的值似乎与for循环方法略有不同

迭代通过1个长度的字符串长字符R

如何自定义3D散点图的图例顺序?

将数字转换为分钟和秒

将二进制数据库转换为频率表

如何将SAS数据集的列名和列标签同时包含在r中GT表的表首?

跨列查找多个时间报告

停止ggplot将多行减少到一行

如何平滑或忽略R中变量的微小变化?

当每个变量值只能 Select 一次时,如何从数据框中 Select 两个变量的组合?

R-找出存在其他变量的各种大小的所有组合