简体   繁体   English

使用ShinyJS使用javascript处理闪亮应用程序中的现有Leaflet地图

[英]Manipulate existing Leaflet map in a shiny app with javascript using shinyjs

I have a shiny app with an existing leaflet map in it. 我有一个带有现有传单地图的闪亮应用程序。 I want to be able to manipulate this map after rendering, using custom javascript through the shinyjs package. 我希望能够在渲染后使用该shinyjs包中的自定义JavaScript来操纵此地图。 A minimal example is below: 下面是一个最小的示例:

app.R app.R

# packages ----------------------------------------------------------------

library(dplyr)
library(leaflet)
library(shiny)
library(shinyjs)

# ui ----------------------------------------------------------------------

ui <- fluidPage(

  useShinyjs(),
  extendShinyjs(script = "my_js.js"),

  leafletOutput(outputId = "map", height = "80vh"),

  tags$hr(),
  tags$p("Button to run the JS code"),
  actionButton(inputId = "go", label = "Add a Marker")

)

# server ------------------------------------------------------------------

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

  # define a simple map
  output$map <- renderLeaflet({

    leaflet() %>%
      addProviderTiles("CartoDB.Positron")
  })

  # observe the go button and run the shinyjs.addMarker function
  observeEvent(
    eventExpr = input$go,
    handlerExpr = js$addMarker()
  )

}

# run ---------------------------------------------------------------------

shinyApp(ui = ui, server = server)

my_js.js my_js.js

shinyjs.addMarker = function(){

  // get the map - this bit doesn't work!
  var map = document.getElementById('map');

  // create a marker and add to map
  var marker = new L.marker([53, -1]).addTo(map);

  // really I'd be going off and querying an API, or doing
  // something else for which there is no handy R function.

};

The question really is how to access the map object after it has been created. 问题实际上是在创建地图对象后如何访问它。 Obviously with this example I'm just adding a marker, which I could do with leafletProxy() , but in reality I want to query an API and bring extra data onto the map when the user performs an action. 显然,在此示例中,我只是添加了一个标记,可以使用leafletProxy() ,但实际上,我想查询API并在用户执行操作时将额外的数据带到地图上。

Any help or advice on other ways to do this would be appreciated! 任何其他方式的帮助或建议,将不胜感激!

Chris 克里斯

You can reach the map object with htmlwidtget 's onRender() function. 您可以使用htmlwidtgetonRender()函数访问地图对象。 Then, you might save it to the globally scoped variable (by skipping the var keyword while creating the variable). 然后,您可以将其保存到全局范围的变量中(通过在创建变量时跳过var关键字)。 This becomes available anywhere in the JavaScript code. 这将在JavaScript代码中的任何位置可用。

output$map <- renderLeaflet({
    leaflet() %>%
        addProviderTiles("CartoDB.Positron") %>%
        htmlwidgets::onRender("
            function(el,x) {
                map = this;
            }
        ")
})

Your my_js.js would then look as follows: 然后,您的my_js.js如下所示:

shinyjs.addMarker = function(){

  // create a marker and add to map
  var marker = new L.marker([53, -1]).addTo(map);

  // really I'd be going off and querying an API, or doing
  // something else for which there is no handy R function.

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM