Display different markers depending on a selectInput field in Shiny

182 views Asked by At

I am trying to display markers on a map, which appearance would depend on a selectInput. The global idea is that the user can click on different locations on a map, searching for an ideal spot. Eventually, when the choice is ok, he validates the location by clicking on a actionbutton, displaying a marker. Without adding the icon shape, everything is working, but when I add icon properties to the addMarkers line, I get a Error in : objet de type 'closure'..., I tried several things depending on Observe, eventReactive, etc... but I'm out of ideas, any help would be much appreciated ! Thank you !

library(shiny)
library(shinythemes)
library(leaflet)

icon.shapes <- c("cylindrical","spherical","cubic")

ui <- navbarPage(
      fluidPage(theme = shinytheme("cerulean"),
            wellPanel(tags$style(type="text/css", '#leftPanel { width:300px; float:left;}'),
                      id = "leftPanel",
                      selectInput("icon_shape", "Icon shape :",
                                  icon.shapes),
            ),
            mainPanel(                          
                      tabsetPanel(
                        tabPanel("Location", leafletOutput("map")),
            )
           )
)
  )

leafletOutput("map", width = "100%", height = "100%")

server = function(input, output, session) {

output$map <- renderLeaflet({
    leaflet() %>%
      addProviderTiles("CartoDB.Positron")%>%
      setView(lng = -4, lat= 52.54, zoom = 7)
  })

observeEvent(input$map_click, {
    click <- input$map_click
    text<-paste("Lattitude ", click$lat, "Longtitude ", click$lng)
    updateNumericInput(session, 'lat', value=input$map_click$lat)
    updateNumericInput(session, 'lng', value=input$map_click$lng)
    proxy <- leafletProxy("map")
    proxy %>% clearPopups() %>%
      addPopups(click$lng, click$lat, text)
    proxy <- leafletProxy("map")
    proxy %>% clearShapes() %>% 
      addCircles(click$lng, click$lat)
  })

  cageIcons <- reactive({
    
    icons(
    iconUrl = ifelse(input$icon_shape == "Cylindrical",
                     "http://www.pngmart.com/files/7/Cylinder-PNG-File.png",
                     ifelse(input$icon_shape == "Sphérical",
                            "https://img.pngio.com/sphere-d-png-sphere-3d-png-transparent-png-6604207-free-3d-sphere-png-840_614.png",
                            "https://www.pinclipart.com/picdir/middle/7-75687_shapes-clipart-cube-pink-cube-png-transparent-png.png")
    ),
    iconWidth = 38, iconHeight = 95,
    iconAnchorX = 22, iconAnchorY = 94,
  )
  return(icons)
  })

observeEvent(input$submit_config, {
    proxy <- leafletProxy("map")
    proxy %>% 
      addMarkers(input$map_click$lng, input$map_click$lat, icon=icons)
  })
}

shinyApp(ui = ui, server = server)
0

There are 0 answers