1

我正在构建一个闪亮的应用程序,使用 Golem 作为框架。在我的应用程序中,我制作了几个模块,所有模块都通过传单地图链接。但是,除了创建地图的模块之外,我无法从另一个模块更新地图。

我已经尝试考虑到这里提出的建议,将map_idand包含parent_session到模块调用中,但是每当我尝试对地图进行任何更改时,应用程序仍然会崩溃(没有错误跟踪)。

这是我的代码的精简版本,在单独的文件中:

app_server.R

#' The application server-side
#' 
#' @param input,output,session Internal parameters for {shiny}. 
#'     DO NOT REMOVE.
#' @import shiny
#' @import leaflet
#' @noRd
app_server <- function( input, output, session ) {
  
  
  r <- reactiveValues(
    map=NULL,
    origin=list(lat=NULL, lng=NULL),
    destination=list(lat=NULL, lng=NULL)
  )
  
  mod_basemap_server("basemap_ui_1", r, session)
  mod_itinerary_server("itinerary_ui_1", r, map_id="basemap", parent_session=session)
}

app_ui.R

#' The application User-Interface
#' 
#' @param request Internal parameter for `{shiny}`. 
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_ui <- function(request) {
  tagList( 
    bootstrapPage(      
      mod_basemap_ui("basemap_ui_1"),
      mod_itinerary_ui("itinerary_ui_1")
      )
    
  )
}

mod_basemap.R

#' basemap UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
mod_basemap_ui <- function(id){
  ns <- NS(id)
  tagList(
    # Map background
    leaflet::leafletOutput(ns("basemap"))    
  )
}
    
#' basemap Server Functions
#'
#' @noRd 
mod_basemap_server <- function(id, r, session){
  shiny::moduleServer( id, function(input, output, session){
    ns <- session$ns
    
    output$basemap <- leaflet::renderLeaflet({
      # generate base leaflet
      map = leaflet::leaflet(options = leaflet::leafletOptions(zoomControl = FALSE)) %>%
        leaflet::addTiles(leaflet::providers$OpenStreetMap) %>%
        leaflet::addProviderTiles(leaflet::providers$OpenStreetMap,
                                  group="Open Street Map") %>% 
        leaflet::addProviderTiles(leaflet::providers$OpenTopoMap,
                                  group="Open Topo Map") %>% 
        leaflet::addProviderTiles(leaflet::providers$Esri.WorldImagery,
                                  group="Esri World Imagery") %>% 
        leaflet::fitBounds(2.78, 44.85, 3.41, 44.71) %>%
        leaflet::addLayersControl(
          baseGroups = c("Open Street Map", "Open Topo Map", "Esri World Imagery")
        )
      map
    })
        
    # Clicks on the map
    observeEvent(input$basemap_click, {
      click = input$basemap_click
      # Check whether we're updating origin marker or destination marker
      if(r$orig_dest_switch=="orig"){
        r$origin$lat = click$lat
        r$origin$lng = click$lng
        # Add origin marker on the map (after removing previously added origin)
        leaflet::leafletProxy('basemap')%>%
          leaflet::clearGroup("origin") %>%
          leaflet::addMarkers(lng=r$origin$lng, lat=r$origin$lat,
                     group="origin")
      } else {
        r$destination$lat = click$lat
        r$destination$lng = click$lng
        # Add destination marker on the map (after removing previously added destination)
        leaflet::leafletProxy('basemap')%>%
          leaflet::clearGroup("destination") %>%
          leaflet::addMarkers(lng=r$destination$lng, lat=r$destination$lat,
                              group="destination")
      }
    })
    
  })
}

mod_itinerary.R

#' itinerary UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
mod_itinerary_ui <- function(id){
  ns <- NS(id)
  tagList(
    absolutePanel(id="vehicleDetails", bottom=10, left=15, h4('Itinerary'),
                  h6("First select an origin on the map, then select a destination before plotting"),
                  style='background-color:white; opacity:0.8;padding: 0 20px 20px 20px',
                  radioButtons(
                    inputId=ns('orig_dest_switch'),
                    h5("Change origin or destination"),
                    choices = c("Origin" = 'orig', "Destination" = 'dest'),
                    inline = TRUE),
                  actionButton(inputId=ns("confirm_itin"), label="Plot itinerary")
    )
  )
  
}

#' itinerary Server Functions
#'
#' @noRd 
mod_itinerary_server <- function(id, r, map_id, parent_session){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    
    # Change origin/destination switch
    observeEvent(input$orig_dest_switch, {
      r$orig_dest_switch = input$orig_dest_switch
    })
    
    # Click on confirm button
    observeEvent(input$confirm_itin, {
    # Delete previous itineraries
    leaflet::leafletProxy(mapId = map_id, session = parent_session) %>%
      leaflet::clearGroup('itin')
    # Show itinerary on map
    leaflet::leafletProxy(mapId = map_id, session = parent_session) %>%
      leaflet::addPolylines(lng=c(r$origin$lng, r$destination$lng),
                            lat=c(r$origin$lat, r$destination$lat),
                            group='itin')
        
    })
    
    
  })
}
shinyApp(app_ui, app_server)
4

1 回答 1

1

我不知道为什么它会为你崩溃,我没有这个问题。不过,主要问题是,app_server.Rmap_id="basemap"您调用模块进行行程时,您输入了。

mod_basemap.R中,该地图确实称为“底图”,但它被包裹在 中ns(),这意味着它的实际 id 是“您在调用模块时给出的名称-”+“您给输入的 id”。因此,此处地图的实际 ID 为“basemap_ui_1-basemap”。

现在,指定整个 id 不是一个好主意(如果稍后将“basemap_ui_1”替换为其他内容会怎样?),所以您想要在调用时返回地图 id mod_basemap.R,以便您可以在其他地方使用此 id模块。因此,在服务器部分的末尾mod_basemap.R,您可以添加:

return(list(map_id = ns("basemap")))

这样你就有了:

mod_basemap_server <- function(id, r, session){
  shiny::moduleServer( id, function(input, output, session){
    ns <- session$ns
    
    # ... Code you already have ...

    return(list(map_id = ns("basemap")))
    
  })
}

In app_server.R, you can now assign the module mod_basemap to an object (that I call basemap), and then use basemap$map_id when you call the module mod_itinerary:

basemap <- mod_basemap_server("basemap_ui_1", r, session)
mod_itinerary_server("itinerary_ui_1", r, map_id = basemap$map_id, parent_session = session)

I hope this is clear. You can also check this RStudio article about passing information between modules.

Last thing, you made a typo in mod_itinerary.R, you wrote r$destination$lng where it should be r$destination$lat.

With that, clicking on two points on the map and then clicking on "Plot itinerary" should display a line between those two points. If you still have some unexpected crashes, I guess this comes from code in other modules that you may have because this code works fine for me.

于 2022-01-03T19:36:42.280 回答