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

问题描述

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

这是可重现的示例。请在地图上绘制矩形,并看到没有坐标返回到文本区域。但是,如果您取消注释我的格雷码(在模块 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,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,session) {  
  mod_btn_server1("test-btn")  
}

shinyApp(ui = ui,server = server)

解决方法

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

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

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

polygon_coords <- reactive(mapPassed())
,

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...