如何访问使用输入传递的传单地图?

How to access leaflet map passed using input?

在我的示例中,我需要从模块 2(嵌套)访问 leaflet 地图,并在地图上使用 input$ 到 return 多边形坐标。当我在我的主模块(模块 1)中执行此操作时,它工作得很好,我可以在其中自由引用 'map'。不幸的是,当我将地图传递给另一个模块(模块 2)时,input$ 的相同技巧根本不起作用。你知道怎么解决吗?

这是可重现的例子。请在地图上绘制矩形并查看没有坐标 returned 到文本区域。但是,如果您取消注释我的灰色代码(在模块 1 中),那么您将看到一切正常。

library(shiny)
library(mapboxer)
library(dplyr)
library(sf)
library(leaflet)
library(leaflet.extras)


moduleServer <- function(id, module) {
  callModule(module, id)
}

# UI 1 #
mod_btn_UI1 <- function(id) {
  
  ns <- NS(id)
  tagList(
    leafletOutput(ns("map")),
    mod_btn_UI2(ns("other")),
    verbatimTextOutput(ns("selectedArea1"))    
  )
}

# Server 1 #
mod_btn_server1 <- function(id){
  moduleServer(id, function(input, output, session) {
    
    ns <- NS(id)
        
    coords <- quakes %>%
      sf::st_as_sf(coords = c("long","lat"), crs = 4326)
    
    output$map <- leaflet::renderLeaflet({
      leaflet::leaflet() %>% 
        leaflet::addTiles() %>% 
        leaflet::setView(172.972965,-35.377261, zoom = 4) %>%
        leaflet::addCircleMarkers(
          data = coords,
          stroke = FALSE,
          radius = 6) %>% 
        leaflet.extras::addDrawToolbar()
    })
    
    # here I pass map id and session to module 2 and it works fine
    mod_btn_server2("other", "map", session)  
    
    # # the code below works fine - here I simply pass 'map' to input$
    #
    # polygon_coords <- reactive({input$map_draw_new_feature$geometry$coordinates[[1]]})
    # output$selectedArea1 <- renderPrint({
    #   print(polygon_coords())
    # })           
  })
}


# UI 2 #
mod_btn_UI2 <- function(id) {
  
  ns <- NS(id)
  tagList(
    verbatimTextOutput(ns("selectedArea2"))
  )
}

# Server 2 #
mod_btn_server2 <- function(id, mapPassed, parentSession){
  moduleServer(id, function(input, output, session) {
    
    ns <- NS(id)
   
  # here input seems not working with 'mapPassed' as it does in module 1   
  polygon_coords <- reactive({input$mapPassed_draw_new_feature$geometry$coordinates[[1]]})
    
     output$selectedArea2 <- renderPrint({
       print(polygon_coords())
     
   })
  })
}


# Final app #

ui <- fluidPage(  
  tagList(
    mod_btn_UI1("test-btn"))  
)

server <- function(input, output, session) {  
  mod_btn_server1("test-btn")  
}

shinyApp(ui = ui, server = server)

当您使用这种模块化时,输入、输出和会话模块参数是主 input/output/session 的子集,对应于从模块 id(NS 函数)创建的命名空间。如果您在 ID 为 foo 的模块中创建一个名为 dummy 的输入,那么真正创建的是一个名为 foo-dummy 的输入。因此,在 foo 模块中,您将获得名称以“foo-”开头的所有输入以及类似地以“foo-”开头的所有输出作为输入。

您可以将输入名称作为反应式传递,而不是将输入名称传递给嵌入式模块:

mod_btn_server2("other", 
                 reactive(input$map_draw_new_feature$geometry$coordinates[[1]]), 
                 session)

并且您可以像使用任何其他反应式一样在嵌入式模块中使用它:

polygon_coords <- reactive(mapPassed())