如何将嵌套模块与传单地图一起使用

How to use nested modules with a leaflet map

我正在尝试使用 shiny 模块构建带有 leaflet 地图的应用程序。但是,当我 运行 地图下方的代码无法正确呈现时 - 它呈灰色显示。

我想要发生的是用户从侧面板中选择城市并显示地图的相关部分(在末尾有一个示例使用标准闪亮的问题来说明)。

我认为可能是用户输入未在模块之间正确传递但不知道如何修复。事实上,如果我更改相关位以对城市进行硬编码 setView(lng = data[data$pt == "London", "lng"], lat = data[data$pt == "London", "lat"], zoom = 9),那么地图就会呈现。

关于如何使用模块执行此操作的任何提示? 这是我使用模块的非工作尝试:

# Some data
data <- data.frame(pt = c("London", "Manchester"), lat=c(51.5, 53.48), lng=c(0.126, -2.24))
  
# Define the side panel UI and server      
sideUI <- function(id) {
                        ns <- NS(id)
                        selectInput(ns("city"), "", choices=data$pt, selected = "London")
                       }

# In this case this server not needed but using uiOuput/renderUI in real case
# sideServer <- function(id) { moduleServer(id,function(input, output, session) { })}
         
# Define the UI and server functions for the map
mapUI <- function(id) {
                    ns <- NS(id)
                    leafletOutput(ns("map"))
                    }    
    
mapServer <- function(id) {
                            moduleServer(
                                id,
                                function(input, output, session) {
                                    output$map <- renderLeaflet({
                                                leaflet() %>%
                                                    addTiles() %>%
                                                    setView(lng = data[data$pt == input$city, "lng"], 
                                                            lat = data[data$pt == input$city, "lat"], 
                                                            zoom = 9) 
                                                    })
                                    })
                                }
    
# Build ui & server and then run
ui <- dashboardPage(
              dashboardHeader(),
              dashboardSidebar(sideUI("side")),
              dashboardBody(mapUI("mapUK"))
          ) 
server <- function(input, output, session) { mapServer("mapUK") }
shinyApp(ui, server)

这是一个使用标准 shiny 函数的工作示例,它显示了我正在尝试做的事情

library(shiny)
library(shinydashboard)
library(leaflet)
ui <- dashboardPage(
              dashboardHeader(),
              dashboardSidebar(selectInput("city", "", choices=data$pt, selected = "London")),
              dashboardBody(leafletOutput("map"))) 
    
server <- function(input, output, session) { 
                output$map <- renderLeaflet({
                                            leaflet() %>%
                                                addTiles() %>%
                                                setView(lng = data[data$pt == input$city, "lng"],
                                                        lat = data[data$pt == input$city, "lat"], 
                                                        zoom = 9) 
                                                })
                                    }
shinyApp(ui, server)

要将输入从一个模块传递到另一个模块,必须 return 来自源的输入并在目标中将它们用作参数。


library(shiny)
library(shinydashboard)
library(leaflet)

# Some data
data <- data.frame(pt = c("London", "Manchester"), lat=c(51.5, 53.48), lng=c(0.126, -2.24))

# Define the side panel UI and server
sideUI <- function(id) {
  ns <- NS(id)
  selectInput(ns("city"), "", choices=data$pt, selected = "London")
}

sideServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {

      # define a reactive and return it
      city_r <- reactiveVal()
      observeEvent(input$city, {
        city_r(input$city)
      })

      return(city_r)
    })
}
# In this case this server not needed but using uiOuput/renderUI in real case
# sideServer <- function(id) { moduleServer(id,function(input, output, session) { })}

# Define the UI and server functions for the map
mapUI <- function(id) {
  ns <- NS(id)
  leafletOutput(ns("map"))
}

mapServer <- function(id, city) {
  moduleServer(
    id,
    function(input, output, session) {
      output$map <- renderLeaflet({
        leaflet() %>%
          addTiles() %>%
          setView(lng = data[data$pt == city(), "lng"],
                  lat = data[data$pt == city(), "lat"],
                  zoom = 9)
      })
    })
}

# Build ui & server and then run
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(sideUI("side")),
  dashboardBody(mapUI("mapUK"))
)
server <- function(input, output, session) {

  # use the reactive in another module
  city_input <- sideServer("side")
  mapServer("mapUK", city_input)

  }
shinyApp(ui, server)