传递给嵌套模块的按钮无法正常工作

Button passed to nested module doesn't work properly

出于某种原因,我需要嵌套模块。我将按钮传递给嵌套模块 mod_coordinates_server,但是当我单击按钮时,地图上没有显示点。我在这里使用 reactive 并且按钮被正确传递 - 当您取消注释 print(btn())(在 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)

也在主应用程序中调用第二个模块。试试这个

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

# UI #
mod_btn_UI <- function(id) {
  
  ns <- NS(id)
  tagList(
    actionButton(ns("btn"), "Click me!")
    
  )
}

# Server #
mod_btn_server <- function(id){
  moduleServer(id, function(input, output, session) {
    
    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 <- session$ns
    
    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"), 
    mod_coordinates_UI("proxyMap")
    )
)

server <- function(input, output, session) {
  
  btn <- mod_btn_server("test-btn")
  mod_coordinates_server("proxyMap", btn)
}

shinyApp(ui = ui, server = server)

如果你想保持嵌套,试试这个

library(shiny)
library(mapboxer)
library(dplyr)
library(sf)

Sys.setenv(MAPBOX_API_TOKEN = "pk.eyJ1IjoiaHdsIiwiYSI6ImNramJxY2YxcDV2YXoyeW40YXlvbmUyazQifQ.7HBEvMyrAnVpkKO7MNH7ww")

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", reactive({input$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 <- session$ns
    
    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)