我正試圖使用shiny模塊來構建一個帶有leaflet地圖的應用程式。然而,當我運行下面的代碼時,地圖不能正常呈現 - 它是灰色的。
我希望發生的情況是,用戶從側板中選擇城市,并顯示地圖的相關部分(在問題的最后有一個使用標準shiny的例子來說明)。
我認為這可能是用戶的輸入沒有在模塊間正確傳遞,但不知道如何解決它。事實上,如果我改變相關的位子來硬編碼城市 setView(lng = data[data$pt == "London", "lng"], lat = data[data$pt == "London", "lat"], zoom = 9) 那么地圖會呈現。
有沒有關于如何使用模塊的提示? 這是我在使用模塊時沒有成功的嘗試:
# Some data。
data <- data. frame(pt = c("London"/span>。 "Manchester")。 lat=c(51. 5, 53. 48), lng=c(0. 126, -2.24))
# 定義側板UI和服務器
sideUI <- function(id) {>
ns <- NS(id)
selectInput(ns("city")。 ""。 choices=data$pt。 選擇= "London")
}
# 在這種情況下不需要這個服務器,但在實際情況下使用uiOuput/renderUI。
# 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) {>
模塊服務器()
id,
function(input, 輸出,會話) {
output$map <- renderLeaflet({)
leaflet()%>%
addTiles() %>%
setView(lng = data[/span>data$pt == input$city。 "lng"],
lat = data[data$pt = input$city。 "lat"],
zoom = 9)
})
})
}
# build ui & server and then run
ui <- dashboardPage(/span>)
dashboardHeader(),
dashboardSidebar(sideUI("side"))。
dashboardBody(mapUI("mapUK")
)
服務器 < - function(input,output。 會話) { 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")))
服務器 < - function(input, 輸出,會話) {
output$map <- renderLeaflet({)
leaflet()%>%
addTiles() %>%
setView(lng = data[/span>data$pt == input$city。 "lng"],
lat = data[data$pt = input$city。 "lat"],
zoom = 9)
})
}
shinyApp(ui, server)。
uj5u.com熱心網友回復:
要把輸入從一個模塊傳到另一個模塊,必須從源模塊回傳,并在目標模塊中作為引數使用。
library(shiny)
library(shinydashboard)
library(leaflet)
# 一些資料
data <- data. frame(pt = c("London"/span>。 "Manchester")。 lat=c(51. 5, 53. 48), lng=c(0. 126, -2.24))
# 定義側板UI和服務器
sideUI <- function(id) {>
ns <- NS(id)
selectInput(ns("city")。 ""。 choices=data$pt。 選擇= "London")
}
sideServer <- function(id) {>
模塊服務器()
id,
function(input, 輸出,會話) {
# define a reactive and return it
city_r <- reactiveVal()
observeEvent(input$city, {)
city_r(input$city)
})
return(city_r)
})。
}
# 在這種情況下不需要這個服務器,但在實際情況下使用uiOuput/renderUI。
# 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) {>
模塊服務器()
id,
function(input, 輸出,會話) {
output$map <- renderLeaflet({)
leaflet()%>%
addTiles() %>%
setView(lng = data[/span>data$pt == city()。 "lng"],
lat = data[data$pt ==城市()。 "lat"],
zoom = 9)
})
})
}
# build ui & server and then run
ui <- dashboardPage(/span>)
dashboardHeader(),
dashboardSidebar(sideUI("side"))。
dashboardBody(mapUI("mapUK")
)
服務器 < - function(input, 輸出,會話) {
# 在另一個模塊中使用反應式
city_input <- sideServer("side")
mapServer("mapUK", city_input)
}
shinyApp(ui, server)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/322185.html
標籤:
