0

I am trying to load shapefiles in shiny r based on a selection users make in selectInput. This is easy to do when the user can only select one shapefile. However when the user can select multiple shapefiles it becomes trickier. I am looking for a way to avoir having to write several times addPolygons(data = input$input_company[1] %>% addPolygons(data = input$input_company[2] %>% addPolygons(data = input$input_company[3] and so on.

Here is my attempt: writing a loop in server :

# Working directory ------------------------------------------------------------
wd <- "~/path/"

# Read multiple shapefiles with standardised name ------------------------------
items <- c("item_1", "item_2", "item_3")

for (sp in items) {
  files.sp <- readOGR(dsn = wd, layer = sp,
                      verbose = FALSE)
  assign(sp, files.sp)
} 

# UI ---------------------------------------------------------------------------
ui <- navbarPage(
  title = "Here my Title", 
  id="nav",
  theme = shinytheme("flatly"),
  mainPanel("Interactive map",
           div(class="outer",
               tags$head(
                 includeCSS("styles.css")),
               leafletOutput("m", width="100%", height="100%"),
               absolutePanel(
                 id = "hist_panel", class = "panel panel-default",
                 fixed = TRUE, draggable = TRUE,
                 top = 100, left = "auto", right = 0,
                 bottom = "auto",
                 width = "27%", height = "auto"),
               absolutePanel(
                 id = "hist_panel", class = "panel panel-default",
                 fixed = FALSE, draggable = TRUE,
                 top = 100, left = "auto", right = 0,
                 bottom = "auto",
                 width = "27%", height = "auto",
  selectInput(inputId = "input_items", label = "Items", 
              choices = c("Item 1" = "item_1", "Item 2" = "item_2", "Item 3" = "item_3"),
              multiple = TRUE, 
              selected = "item_1")),
    )
  )
)

# Server -----------------------------------------------------------------------
server <- function(input, output, session) {
  
  output$m <- renderLeaflet({
    for (i in 1:length(input$input_items)) {
      sp <- input$input_items[i]
      tmp <- get(sp)
      
    m <- leaflet() %>%
      # Add Basemap OSM
      addTiles(group = "OSM (default)") %>%
      addPolygons(data = get(tmp))
      }
    }
  )
} 


# Run shiny app on laptop
shinyApp(ui, server)

What I am trying to avoid is this (because I may have 100+ items to display, and also because if the user selects less than 3 items I get an error message...):

# Server -----------------------------------------------------------------------
server <- function(input, output, session) {
  
  output$m <- renderLeaflet({

    m <- leaflet() %>%
      # Add Basemap OSM
      addTiles(group = "OSM (default)") %>%
      addPolygons(data = input$input_items[1]) %>%
      addPolygons(data = input$input_items[2]) %>%
      addPolygons(data = input$input_items[3])
    }
  )
} 

Thank you!

Marcel Campion
  • 247
  • 1
  • 7
  • Try `addPolygons(data %in% input$input_items)` – YBS Jul 28 '21 at 15:03
  • Hi @YBS, thanks a lot for your reply. Unfortunately I get the following Error message ```addPolygons must be called with both lng and lat, or with neither.``` – Marcel Campion Jul 28 '21 at 15:14
  • Perhaps the answer [here](https://stackoverflow.com/questions/68491925/filter-out-areas-from-json-file) might help you. – YBS Jul 28 '21 at 15:42

1 Answers1

0

Here is a solution First merge shapefiles together

shp <- bind(item_1, item_2, item_3)

Then in server side:

# Server -----------------------------------------------------------------------
server <- function(input, output, session) {

  observeEvent(input$input_items,{      
    
  sel_shp <- shp[shp@data$id %in% input$input_items, ]

  output$m <- renderLeaflet({
        m <- leaflet() %>%
    # Add Basemap OSM
      addTiles(group = "OSM (default)") %>%
      addPolygons(data = sel_shp)
  
        })  
  })
}
Marcel Campion
  • 247
  • 1
  • 7