0

I have been trying to create an interactive map with sliders and drop down menu's for the NLD_muni dataset from the spData package. I am new to shiny, how can i change this code to make my interactive app show these variables in a more useful way.

At the moment. When selecting population and origin, it only reflects the origin when you click on various locations - and does not show any information about population.

library(spData)

data(NLD_muni)

pop_vars <- setdiff(names(NLD_muni), c("code", "name", "province", "geometry", "origin_native", "origin_west", "origin_non_west", "population", "pop_men", "pop_women"))
origin_vars <- setdiff(names(NLD_muni), c ("code", "name", "province", "geometry", "population", "pop_0_14", "pop_15_24", "pop_25_44", "pop_45_64", "pop_65plus", "pop_men", "pop_women"))


ui <- fluidPage(
  tmapOutput("map"),
  selectInput("pop", "Population", pop_vars),
  selectInput("or", "Origin", origin_vars)
)

server <- function(input, output, session) {
  output$map <- renderTmap({
    tm_shape(NLD_muni) +
      tm_polygons(world_vars[1]) 
  })
  
  output$map <- renderTmap({
    tm_shape(NLD_muni) +
      tm_polygons(origin_vars[1])
  })
  
  observe({
    pop <- input$pop
    tmapProxy("map", session, {
      tm_shape(NLD_muni) +
        tm_polygons(pop)
    
    })
    
  observe ({
    or <- input$or
    tmapProxy("map", session, {
      tm_shape(NLD_muni) +
        tm_polygons(or)
    })
  })
  })
}   


shinyApp(ui, server)


Sean
  • 47
  • 6

1 Answers1

0

I think what you want is to display a map showing the most recently selected item from either of the drop downs. So if you select pop_65plus, the data for this will be displayed and then if you select origin_west, the data for this will be displayed causing the pop_65plus data to be removed.

Here's some code that does this.

library(spData)
library(shiny)
library(tmap)

data(NLD_muni)

pop_vars <- setdiff(names(NLD_muni), c("code", "name", "province", "geometry", "origin_native", "origin_west", "origin_non_west", "population", "pop_men", "pop_women"))
origin_vars <- setdiff(names(NLD_muni), c ("code", "name", "province", "geometry", "population", "pop_0_14", "pop_15_24", "pop_25_44", "pop_45_64", "pop_65plus", "pop_men", "pop_women"))

ui <- fluidPage(
    tmapOutput("map"),
    selectInput("pop", "Population", pop_vars),
    selectInput("or", "Origin", origin_vars)
)

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

    output$map <- renderTmap({
        # explicily set the index of the layer so it can be removed later
        tm_shape(NLD_muni) + tm_polygons(origin_vars[1], zindex = 401) 
    })
    
    observe({
        pop <- input$pop
        tmapProxy("map", session, {
            tm_remove_layer(401)
            tm_shape(NLD_muni) + tm_polygons(pop, zindex = 401)
        })
    }) # make this observe no longer include the following one
    observe ({
        or <- input$or
        tmapProxy("map", session, {
            tm_remove_layer(401)
            tm_shape(NLD_muni) + tm_polygons(or, zindex = 401)
        })
    })
}   

shinyApp(ui, server)

I changed the original code so that one of the observe statements is not nested inside the other. I also explicitly removed a layer each time a new layer is added. I have to be honest and say that it worked without this but in the interests of completeness, I included the extras. To make the example reproducible, I also added some library calls and removed one call to renderTmap that was using a variable world_vars that didn't exist.

Andrew Chisholm
  • 6,362
  • 2
  • 22
  • 41