如何正确实现输入$ map_marker_click?

时间:2017-03-05 21:04:04

标签: r shiny

我正在尝试将shinydashboardleatlef一起使用。阅读完这篇文章on SO后,我尝试了几项没有成功的事情,通过鼠标点击事件在传单地图中的标记上显示来自df的相应信息。

示例数据:

latitude<-c(35.94077, 35.83770, 35.84545, 35.81584, 35.79387, 36.05600)
longitude<-c(-78.58010, -78.78084, -78.72444, -78.62568, -78.64262,-78.67600)
amounts1<-c(27, 44, 34, 46, 25, 15)
amounts2<-c(34, 52, 35, 78, 14, 24)
ids<-c("a", "b", "c", "d", "e", "f")
df<-data.frame(ids,amounts1,amounts2,latitude,longitude)

我的代码如下:

library(shiny)
library(shinydashboard)
library(leaflet)

    ui = dashboardPage(
      dashboardHeader(title = "Testing leatlef"),
      dashboardSidebar(


        sidebarMenu(menuItem(
          "Dashboard",
          tabName = "dashboard",
          icon = icon("dashboard")
        ))
      ),
      dashboardBody(
        tags$script(HTML("addClass(‘sidebar-mini’);")),

        tags$head(
          tags$link(rel = "stylesheet", type = "text/css", href = "style.css")
        ),

        tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),

        fluidRow(column(width = 12,
                        leafletOutput('map', height = 550))),

        fluidRow(verbatimTextOutput("Click_text"))
      )
    )
    ################################################################################
    # Server
    ################################################################################

    server = function(input, output, session) {

      map = createLeafletMap(session, 'map')

      session$onFlushed(once = T, function() {

      output$map <- renderLeaflet({
          leaflet(df) %>%
            addMarkers( ~ longitude,
                        ~ latitude)
        })

      })


      observe({
        click <- input$map_marker_click
        if (is.null(click))
          return()
        text <-
          paste("Lattitude ",
                click$latitude,
                "Longtitude ",
                click$longtitude)
        map$clearPopups()
        map$showPopup(click$latitude, click$longtitude, text)
      })





    }
    ################################################################################
    # run app
    shinyApp(ui, server)

1 个答案:

答案 0 :(得分:5)

您的click事件按预期触发,但您需要更改使用方式。

  1. 纬度/经度值存储在click$latclick$lng
  2. 您需要使用leafletProxy来更新地图。您不能只重复使用map对象
  3. 在名为showPopups的传单中也没有任何功能。您需要使用addPopups()
  4. 您的server看起来像

    server = function(input, output, session) {
    
        latitude<-c(35.94077, 35.83770, 35.84545, 35.81584, 35.79387, 36.05600)
        longitude<-c(-78.58010, -78.78084, -78.72444, -78.62568, -78.64262,-78.67600)
        amounts1<-c(27, 44, 34, 46, 25, 15)
        amounts2<-c(34, 52, 35, 78, 14, 24)
        ids<-c("a", "b", "c", "d", "e", "f")
        df<-data.frame(ids,amounts1,amounts2,latitude,longitude)
    
        map = createLeafletMap(session, 'map')
    
        session$onFlushed(once = T, function() {
    
            output$map <- renderLeaflet({
                leaflet(df) %>%
                    addMarkers( ~ longitude,~ latitude)
            })
        })
    
        observe({
            click <- input$map_marker_click
            if (is.null(click))
                return()
    
            print(click)
            text <-
                paste("Lattitude ",
                            click$lat,
                            "Longtitude ",
                            click$lng)
    
            leafletProxy(mapId = "map") %>%
                clearPopups() %>%
                addPopups(dat = click, lat = ~lat, lng = ~lng, popup = text)
    
            # map$clearPopups()
            # map$showPopup(click$latitude, click$longtitude, text)
        })
    }
    
    shinyApp(ui, server)
    
相关问题