出于某种原因,我需要嵌套模块。我将按钮传递给嵌套模块mod_coordinates_server
,但是当我单击按钮点时,地图上没有显示。我在此处使用reactive
并且按钮已正确传递 - 当您取消注释print(btn())
(in observeEvent
) 时,您会看到值已正确返回到控制台。这意味着该按钮可以正常工作。那么为什么它对积分不起作用?命名空间有问题吗?
下面的工作示例:
library(shiny)
library(mapboxer)
library(dplyr)
library(sf)
moduleServer <- function(id, module) {
callModule(module, id)
}
# UI #
mod_btn_UI <- function(id) {
ns <- NS(id)
tagList(
actionButton(ns("btn"), "Click me!"),
mod_coordinates_UI(ns("proxyMap"))
)
}
# Server #
mod_btn_server <- function(id){
moduleServer(id, function(input, output, session) {
btn <- reactive({input$btn})
mod_coordinates_server("proxyMap", btn)
})
}
# Module Coordinates
mod_coordinates_UI <- function(id) {
ns <- NS(id)
tagList(
mapboxerOutput(ns("map"))
)
}
mod_coordinates_server <- function(id, btn){
moduleServer(id, function(input, output, session) {
ns <- NS(id)
coords <- quakes %>%
sf::st_as_sf(coords = c("long","lat"), crs = 4326)
output$map <- mapboxer::renderMapboxer({
mapboxer::mapboxer(
style = mapboxer::basemaps$Mapbox$light_v10,
center = c(174.387636,-33.543557),
pitch = 5,
padding = 0,
zoom = 3)
})
observeEvent(btn(), {
# print(btn())
mapboxer::mapboxer_proxy(ns("map")) %>%
mapboxer::add_circle_layer(
id = "xyz",
source = mapboxer::as_mapbox_source(coords),
circle_color = "#952444",
circle_opacity = 0.7,
circle_radius = 6
) %>%
mapboxer::update_mapboxer()
})
})
}
# FINAL App #
ui <- fluidPage(
tagList(
mod_btn_UI("test-btn"))
)
server <- function(input, output, session) {
mod_btn_server("test-btn")
}
shinyApp(ui = ui, server = server)