1

我正在尝试使用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)

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

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)
4

1 回答 1

2

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


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)
于 2021-09-20T20:39:20.750 回答