r-是否可以将传单地图传递到另一个模块



我写了一个应用程序,试图将其模块化。一般来说,我在应用程序的主体中添加了传单地图(在主模块中(,我想做的是编写一些其他模块,这些模块引用了我的主地图(在地图上显示/隐藏点和其他空间操作(。我试着从其他模块中引用这个地图(在主模块中(。在下面的例子中,我将地图作为反应表达式从主模块中传递,但当我按下显示地图上的点的按钮时,错误就会显示出来:

Error in if: missing value where TRUE/FALSE needed

是否可以将映射传递到另一个模块?在那里使用leafletProxy

以下是可复制的示例:

library(shiny)
library(dplyr)
library(sf)
library(leaflet)

moduleServer <- function(id, module) {
callModule(module, id)
}
# Main module - UI 1 #
mod_btn_UI1 <- function(id) {

ns <- NS(id)
tagList(
leafletOutput(ns("map")),
mod_btn_UI2(ns("other"))
)
}
# Main module - Server 1 #
mod_btn_server1 <- function(id){
moduleServer(id, function(input, output, session) {

ns <- NS(id)

# here I pass map as reactive
passMap = reactive({input$map})

coords <- quakes %>%
sf::st_as_sf(coords = c("long","lat"), crs = 4326)


output$map <- leaflet::renderLeaflet({
leaflet::leaflet() %>% 
leaflet::addTiles() %>% 
leaflet::setView(172.972965,-35.377261, zoom = 4) %>%
leaflet::addCircleMarkers(
data = coords,
stroke = FALSE,
radius = 6)
})

mod_btn_server2("other", passMap)  


})
}

# Other module - UI 2 #
mod_btn_UI2 <- function(id) {

ns <- NS(id)
tagList(
actionButton(inputId = ns("btn"), label = "show points")
)
}

# Other module - Server 2 #
mod_btn_server2 <- function(id, passMap){
moduleServer(id, function(input, output, session) {

ns <- NS(id)

coords <- quakes %>%
sf::st_as_sf(coords = c("long","lat"), crs = 4326)

observeEvent(input$btn, {
leaflet::leafletProxy(passMap()) %>%
leaflet::addCircleMarkers(
data = coords,
stroke = TRUE,
color = "red",
radius = 6)
})

})
}

# Final app #
ui <- fluidPage(

tagList(
mod_btn_UI1("test-btn"))
)
server <- function(input, output, session) {

mod_btn_server1("test-btn")

}
shinyApp(ui = ui, server = server)

似乎需要将传单代理作为响应传递给模块,使其工作。

在模块mod_btn_server1:中添加了传单代理,而不是变量passMap的输入名称

proxymap <- reactive(leafletProxy('map'))  
mod_btn_server2("other", proxymap)  

在mod_btn_server2中,您必须将Passmap更改为leafletproxy本身,从而删除对leafletpoxy:的调用

observeEvent(input$btn, {
passMap() %>%
leaflet::addCircleMarkers(
data = coords,
stroke = TRUE,
color = "red",
radius = 6)

})

这里的完整代码:

library(shiny)
library(dplyr)
library(sf)
library(leaflet)

moduleServer <- function(id, module) {
callModule(module, id)
}
# Main module - UI 1 #
mod_btn_UI1 <- function(id) {

ns <- NS(id)
tagList(
leafletOutput(ns("map")),
mod_btn_UI2(ns("other"))
)
}
# Main module - Server 1 #
mod_btn_server1 <- function(id){
moduleServer(id, function(input, output, session) {

ns <- NS(id)

# here I pass map as reactive
passMap = reactive({input$map})

coords <- quakes %>%
sf::st_as_sf(coords = c("long","lat"), crs = 4326)


output$map <- leaflet::renderLeaflet({
leaflet::leaflet() %>% 
leaflet::addTiles() %>% 
leaflet::setView(172.972965,-35.377261, zoom = 4) %>%
leaflet::addCircleMarkers(
data = coords,
stroke = FALSE,
radius = 6)
})
proxymap <- reactive(leafletProxy('map'))  
mod_btn_server2("other", proxymap)  


})
}

# Other module - UI 2 #
mod_btn_UI2 <- function(id) {

ns <- NS(id)
tagList(
actionButton(inputId = ns("btn"), label = "show points")
)
}

# Other module - Server 2 #
mod_btn_server2 <- function(id, passMap){
moduleServer(id, function(input, output, session) {

ns <- NS(id)

coords <- quakes %>%
sf::st_as_sf(coords = c("long","lat"), crs = 4326)

observeEvent(input$btn, {
passMap() %>%
leaflet::addCircleMarkers(
data = coords,
stroke = TRUE,
color = "red",
radius = 6)

})

})
}

# Final app #
ui <- fluidPage(

tagList(
mod_btn_UI1("test-btn"))

)
server <- function(input, output, session) {

mod_btn_server1("test-btn")

}
shinyApp(ui = ui, server = server)

最新更新