r - 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)
解决方案
这是一个解决方案:
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()。
并证明它有效:
推荐阅读
- java - 未发送 SIP/HTTP 融合应用程序邀请
- java - Java wsimport 错误
- php - 可以得到一个由字符串标签定义的随机数吗?喜欢{1-100}
- mysql - 在 Select 语句期间引用一个字段声明的名称
- android - 如何创建一个上面有一些空间(间隙)的布局。不在布局中
- terraform - 将私有 IP 添加到文件 - Terraform
- ember.js - 与 2 API 出现错误的关系
- .net - Cosmos DB:集合存在,但抛出 410 GONE httpstatus 代码
- python - 如何在 Python 3、pandas 中计算总工作时间
- android - 带背景的扁平按钮,或带边框的透明背景