将htmlwidget添加到传单对象的弹出窗口中

时间:2017-01-14 01:22:49

标签: r shiny leaflet dygraphs htmlwidgets

我使用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)

1 个答案:

答案 0 :(得分:3)

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

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

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