R Leaflet PopupGraph - 在 map_marker_click 上 addPopupGraphs [英] R Leaflet PopupGraph - addPopupGraphs on map_marker_click

查看:93
本文介绍了R Leaflet PopupGraph - 在 map_marker_click 上 addPopupGraphs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 r 传单和 Leafpop 库在 map_marker_click 上打开一个弹出窗口,其中包含我的每个标记的唯一绘图.

I would like to open a popup with a unique plot for each of my marker in it on a map_marker_click using r leaflet and the leafpop library.

对于每个点,当用户单击它们时,将计算要显示的图.

For each point when the user click on them the plot to display is computed.

下面是一个可重现的代码,但它不会返回任何错误.
有什么想法吗?

Below is a reproductible code but it doesn't return any error.
Any ideas?

library(tidyverse)
library(ggplot2)
library(shiny)
library(leaflet)
library(leafpop)


id <- c(1,1,1,1,2,2,3,3,3,4)
lat <- c(49.823, 49.823, 49.823, 49.823, 58.478, 58.478, 57.478 , 57.478 , 57.478, 38.551)
lng <- c(-10.854, -10.854, -10.854, -10.854, -11.655, -11.655, 2.021 , 2.021 , 2.021, 5.256)
type <- c("A","C","B","B","C","A","B","A","C","B")
date <- c(152.5,307.5,145,481,152,109.5,258.5,107.5,186.5,150)
start <- c(123,235,135,192,149,101,205,75,155,100)
stop <- c(182,380,155,289,155,218,312,140,218,200)
myData <- data.frame(id,type,date,start,stop,lat,lng)


chronogramme<- function(dataId){

  dataFiltered<-filter(myData,id==dataId)

  p<- ggplot(dataFiltered,aes(type,date))+
    geom_linerange(aes(ymin=start,ymax=stop),size=5)+
    coord_flip()
  return(p)
}


ui <- fluidPage(
  leafletOutput("map"),
  plotOutput("plot")
)


server <- function(input, output, session) {

  #Sortie map
  output$map <- renderLeaflet({
    leaflet()%>%
      addProviderTiles(providers$CartoDB.Positron) %>% 
      addCircleMarkers(
        layerId=~id,
        data = myData,
        lat = myData$lat,
        lng = myData$lng,
        radius = 5,
        color = 'blue',
        stroke = FALSE,
        fillOpacity = 1,
        group = 'markers'
      )
  })

  observeEvent(input$map_marker_click,{
    p <- chronogramme(input$map_marker_click$id)
    isolate({
      leafletProxy("map") %>% addPopupGraphs(list(p), group = 'markers')
    })
  })

}

# Create Shiny app ----
shinyApp(ui = ui, server = server)

推荐答案

感谢您的回复,问题是我的应用程序中有很多数据,因此迭代所有绘图不起作用.

Thank you for your response, the problem is that I have many many data on my application so iterate all the plot doesn't work.

但是,我找到了另一种解决方案:将每个创建的图临时存储为 svg,并使用 addPopus() 显示它们:

However, I've found another solution : store each created plot temporarily as svg, and display them with addPopus() :

library(tidyverse)
library(ggplot2)
library(shiny)
library(leaflet)
library(leafpop)
library(lattice)


id <- c(1,1,1,1,2,2,3,3,3,4)
lat <- c(49.823, 49.823, 49.823, 49.823, 58.478, 58.478, 57.478 , 57.478 , 57.478, 38.551)
lng <- c(-10.854, -10.854, -10.854, -10.854, -11.655, -11.655, 2.021 , 2.021 , 2.021, 5.256)
type <- c("A","C","B","B","C","A","B","A","C","B")
date <- c(152.5,307.5,145,481,152,109.5,258.5,107.5,186.5,150)
start <- c(123,235,135,192,149,101,205,75,155,100)
stop <- c(182,380,155,289,155,218,312,140,218,200)
myData <- data.frame(id,type,date,start,stop,lat,lng)

folder <- tempfile()
dir.create(folder)

chronogramme<- function(dataId){

  dataFiltered<-filter(myData,id==dataId)

  p<- ggplot(dataFiltered,aes(type,date))+
    geom_linerange(aes(ymin=start,ymax=stop),size=5)+
    coord_flip()
  return(p)
}


ui <- fluidPage(
  leafletOutput("map")
)


server <- function(input, output, session) {

  #Sortie map
  output$map <- renderLeaflet({
    leaflet()%>%
      addProviderTiles(providers$CartoDB.Positron) %>% 
      addCircleMarkers(
        layerId=~id,
        data = myData,
        lat = myData$lat,
        lng = myData$lng,
        radius = 5,
        color = 'blue',
        stroke = FALSE,
        fillOpacity = 1
      )
  })

  # When map is clicked, show a popup with city info
  showPopup <- function(id, lat, lng) {
    chrngr <- chronogramme(id)
    svg(filename= paste(folder,"plot.svg", sep = "/"), 
        width = 500 * 0.005, height = 300 * 0.005)
    print(chrngr)
    dev.off()

    content <- paste(readLines(paste(folder,"plot.svg",sep="/")), collapse = "")

    leafletProxy("map") %>% addPopups(lng, lat, content, layerId = id)
  }

  observe({
    leafletProxy("map") %>% clearPopups()
    event <- input$map_marker_click
    if (is.null(event))
      return()

    isolate({
      showPopup(event$id, event$lat, event$lng)
    })
  })

}



# Create Shiny app ----
shinyApp(ui = ui, server = server)

这篇关于R Leaflet PopupGraph - 在 map_marker_click 上 addPopupGraphs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆