将闪亮的散点图添加到传单弹出窗口

时间:2020-04-08 12:38:43

标签: r shiny leaflet r-leaflet

嘿,我想向传单弹出窗口添加一个闪亮的散点图,我想知道这是否有可能,如果可以,怎么办? 到目前为止,我的代码如下:

library(leaflet)
library(shiny)

ui <- fluidPage(
  tags$div(id = "garbage"),  # Copy this disposal-div
  leafletOutput("map"),
  plotOutput("scatterplot", width = "250px", height = "250px"),
  div(id = "Showcase")
)

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

  output$scatterplot <- renderPlot({plot(c(1,2,3), c(1,2,3))})
  output$popup1 <- renderUI({plotOutput("scatterplot",width = "50px", height = "50px")})

  popupMaker <- function(id) {
      as.character(uiOutput(id))
  };

  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      addMarkers(0, 51.4750, popup = popupMaker('popup1')) %>%
          # Copy this part - it initializes the popups after the map is initialized
      htmlwidgets::onRender(
        'function(el, x) {
          var target = document.querySelector(".leaflet-popup-pane");

          var observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
              if(mutation.addedNodes.length > 0){
                Shiny.bindAll(".leaflet-popup-content");
              }
              if(mutation.removedNodes.length > 0){
                var popupNode = mutation.removedNodes[0];

                var garbageCan = document.getElementById("garbage");
                garbageCan.appendChild(popupNode);

                Shiny.unbindAll("#garbage");
                garbageCan.innerHTML = "";
              }
            }); 
          });

          var config = {childList: true};

          observer.observe(target, config);
        }')
  })
}

shinyApp(ui, server)

主要从此处改编:https://stackoverflow.com/a/54381819/3749379

此:

  output$popup1 <- renderUI({
    plotOutput("scatterplot",width = "50px", height = "50px")
  })

只会导致一个空的弹出窗口

0 个答案:

没有答案