首页 > 解决方案 > 根据 Shiny 中的 selectInput 字段显示不同的标记

问题描述

我正在尝试在地图上显示标记,其外观将取决于 selectInput。全球理念是用户可以点击地图上的不同位置,搜索理想地点。Eventually, when the choice is ok, he validates the location by clicking on a actionbutton, displaying a marker. 在不添加图标形状的情况下,一切正常,但是当我将图标属性添加到 addMarkers 行时,出现错误:objet de type 'closure'...,我尝试了几件事,具体取决于 Observe、eventReactive 等。 .但我没有想法,任何帮助将不胜感激!谢谢 !

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)

标签: rshinyleafletmarkersselectinput

解决方案


推荐阅读