单击 Leaflet 地图上的点以在 Shiny 中生成 ggplot
Click on points on Leaflet map to generate ggplot in Shiny
在 R 中使用 Shiny,我正在尝试创建一个 Leaflet 地图,它允许用户单击任何标记以生成代表该特定站点信息(温度)的相应图表。
我合并了这个问题 () and the second trick on this blog (https://www.r-bloggers.com/4-tricks-for-working-with-r-leaflet-and-shiny/) 的代码,但似乎仍然无法在 Shiny 中成功注册单击的标记点。
即当我点击任何网站时没有任何情节。
我无法根据进一步的研究找到任何解决方案,感谢您的帮助。
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().
并证明它有效:
在 R 中使用 Shiny,我正在尝试创建一个 Leaflet 地图,它允许用户单击任何标记以生成代表该特定站点信息(温度)的相应图表。
我合并了这个问题 (
即当我点击任何网站时没有任何情节。
我无法根据进一步的研究找到任何解决方案,感谢您的帮助。
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().
并证明它有效: