首页 > 解决方案 > Click on points on Leaflet map to generate ggplot in Shiny

问题描述

Using Shiny in R, I am attempting to create a Leaflet map which allows the user to click on any markers to generate a corresponding plot that represents the information (temperature) at that specific site.

I incorporated codes from this question (Click on points in a leaflet map as input for a plot in shiny) and the second trick on this blog (https://www.r-bloggers.com/4-tricks-for-working-with-r-leaflet-and-shiny/) but still cannot seem to successfully register the clicked marker point in Shiny.

i.e. Nothing plots when I click on any site.

I could not find any solutions based on further research, any help is appreciated.

library(leaflet)
library(shiny)
library(ggplot2)

# example data frame 
wxstn_df <- data.frame(Site = c("a", "a", "b"), Latitude = c(44.1, 44.1, 37), Longitude = c(-110.2, -110.2, -112.7), Month = c(1,2,1), Temp_avg = c(10, 18, 12))

ui <- fluidPage(column(7, leafletOutput("wsmap", height = "600px")),
  column(5, plotOutput("plot", height = "600px"))
)

server <- function(input, output) {

  # create a reactive value to store the clicked site
  stn <- reactiveValues(clickedMarker = NULL)

  ## leaflet map
  output$wsmap <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      addCircleMarkers(data = wxstn_df, ~unique(Longitude), ~unique(Latitude), layerId = ~unique(Site), popup = ~unique(Site)) 
  })

 # store the click
  observeEvent(input$map_marker_click, {
    stn$clickedMarker <- input$map_marker_click
  })

output$plot <- renderPlot({
      ggplot(wxstn_df[wxstn_df$Site %in% stn$clickedmarker$Site,], aes(Month, Temp_avg)) +
        geom_line()
  }) 
}

shinyApp(ui, server)

标签: rggplot2shinyleafletinteractive

解决方案


这是一个解决方案:

library(leaflet)
library(shiny)
library(ggplot2)

# example data frame 
wxstn_df <- data.frame(Site = c("a", "a", "b"), Latitude = c(44.1, 44.1, 37), Longitude = c(-110.2, -110.2, -112.7), Month = c(1,2,1), Temp_avg = c(10, 18, 12))

ui <- fluidPage(column(7, leafletOutput("wsmap", height = "600px")),
                column(5, plotOutput("plot", height = "600px"))
)

server <- function(input, output) {

  ## leaflet map
  output$wsmap <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      addCircleMarkers(data = wxstn_df, ~unique(Longitude), ~unique(Latitude), layerId = ~unique(Site), popup = ~unique(Site)) 
  })

  # generate data in reactive
  ggplot_data <- reactive({
    site <- input$wsmap_marker_click$id
    wxstn_df[wxstn_df$Site %in% site,]
  })

  output$plot <- renderPlot({
    ggplot(data = ggplot_data(), aes(Month, Temp_avg)) +
      geom_line()
  }) 
}

shinyApp(ui, server)

主要问题是您没有更改您使用的示例中的对象名称,例如 input$wsmap_marker_click 因为 wsmap 是您传单 ID 的名称。同样,要访问站点信息,请使用 input$wsmap_marker_click$id 而不是 input$wsmap_marker_click$Site。在响应函数中打印对象以探索输入对象的外观以及如何访问它的一部分通常很有用。

例如

   # generate data in reactive
  ggplot_data <- reactive({
    print(input$wsmap_marker_click)
    site <- input$wsmap_marker_click$id
    print(site)

    data <- wxstn_df[wxstn_df$Site %in% site,]
    print(data)
    data})

就个人而言,在这种情况下,我更喜欢使用响应式表达式从标记单击生成 ggplot 数据 (ggplot_data()),而不是创建 reactiveValues 对象。每次单击标记时,绘图都会更新为新的 ggplot_data()。

并证明它有效:

在此处输入图像描述


推荐阅读