[英]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. 我正在R中使用Shiny,试图创建一个Leaflet贴图,该贴图允许用户单击任何标记以生成表示该特定位置的信息(温度)的对应图。
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. 我结合了这个问题的代码( 单击传单地图上的点作为闪亮的图的输入 )和此博客的第二个技巧( https://www.r-bloggers.com/4-tricks-for-working-使用-r-leaflet-and-shiny / ),但似乎仍无法在Shiny中成功注册单击的标记点。
ie 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)
Here's a solution: 这是一个解决方案:
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)
The main problem is that you were not changing the object names from the example that you were using, eg input$wsmap_marker_click because wsmap is the name of you leaflet ID. 主要问题是您没有从所使用的示例中更改对象名称,例如input $ wsmap_marker_click,因为wsmap是您的传单ID的名称。 Similarly, to access Site info, use input$wsmap_marker_click$id not input$wsmap_marker_click$Site.
同样,要访问站点信息,请使用input $ wsmap_marker_click $ id,而不要输入$ wsmap_marker_click $ Site。 It is often useful to print the objects within the reactive function to explore what the input object looks like and how to access parts of it.
在反应函数中打印对象通常很有用,以探索输入对象的外观以及如何访问其中的一部分。
eg 例如
# 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})
Personally in this situation I would prefer to use a reactive expression generate ggplot data (ggplot_data()) from marker click rather than creating a reactiveValues object. 就个人而言,在这种情况下,我宁愿使用反应式表达式通过单击标记来生成ggplot数据(ggplot_data()),而不是创建reactactiveValues对象。 Every time the marker is clicked the plot will update with new ggplot_data().
每次单击标记时,图将使用新的ggplot_data()更新。
And proof it works: 并证明它有效:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.