【问题标题】:adding an htmlwidget to a the popup of a leaflet object将 htmlwidget 添加到传单对象的弹出窗口
【发布时间】:2023-03-13 09:39:01
【问题描述】:

我正在使用 R 来组合传单对象和 dygraph。

我特别希望 dygraph(理想情况下是任何 htmlwidget)显示为地图上点的弹出窗口。

以下是我大致尝试生成的代码。

理想情况下,当用户点击标记时,dygraph 应该出现在弹出窗口中。

  # example dygraph
  library(dygraphs)
  lungDeaths <- cbind(mdeaths, fdeaths)
  dygraph(lungDeaths)
  dd <- .Last.value

  # example leaflet map
  library(leaflet)
  leaflet() %>%
    addTiles() %>% 
    addMarkers(lng=174.768, lat=-36.852, 
               popup="The birthplace of R")
  ll <- .Last.value

  # I would like to be able to incorporate the dygraph as the popup,
  # however the below doesn't work.
  ll %>% addMarkers(lng=174.769, lat=-36.853,
                    popup=dd)


  # the end goal is to have this combined in a shiny output
  # below is a rough skeleton to add what I would expect to code
  # in order to be able to add the dygraph into the popup
  library(shiny)
  library(leaflet)
  library(dygraphs)

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

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

    output$mymap <- renderLeaflet({
      leaflet() %>%
        addTiles() %>% 
        addMarkers(lng=174.768, lat=-36.852, 
                   popup=dygraph(lungDeaths))

    })
  }

  shinyApp(ui, server)

【问题讨论】:

    标签: r shiny leaflet dygraphs htmlwidgets


    【解决方案1】:

    这可以使用mapview::popupGraph 来完成,它允许在弹出窗口中包含静态以及基于 htmlwidgets 的图形。您的示例中要更改的相关行是:

    ll %>% addMarkers(lng=174.769, lat=-36.853,
                      popup=mapview::popupGraph(dd, type = "html"))
    

    请注意,这些 html 弹出窗口是通过 iframe 嵌入的,因此可能会出现一些意外行为,尤其是在空间不足时。使用 leaflet 的 github 版本,您可以更改弹出窗口的大小以满足您的需要。 CRAN 版本只允许 300px 的宽度(如果我没记错的话)。

    【讨论】:

    • 谢谢!对于大量的点,您对如何为每个点获取不同的图表有什么建议吗?
    • 尝试传递图表列表。请参阅?popupGraph 中的### example: html -----(最后一个示例)。尽管请注意,对于大量的点,当您将大量 html 代码塞进一个页面时,这可能会变得非常缓慢且无响应。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 2018-12-22
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多