我正在构建一个闪亮的应用程序,使用 Golem 作为框架。在我的应用程序中,我制作了几个模块,所有模块都通过传单地图链接。但是,除了创建地图的模块之外,我无法从另一个模块更新地图。
我已经尝试考虑到这里提出的建议,将map_id
and包含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)