我尝试在单独的标签项中渲染Map。当我单独渲染Map时,它工作得很好。但是当我想在单独的标签项中渲染Map时,每个标签项都与一个侧边栏菜单项相关联,我没有得到任何Map。我尝试使用leafletProxy
,但效果不佳。
下面是说明我的案例的代码:
library(tidyverse)
library(shiny)
library(leaflet)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = 'Interactive Maps'),
dashboardSidebar(
sidebarMenu(
menuItem("1st Map", tabName = "my_map1"),
menuItem("2nd Map", tabName = "my_map2")
)
),
dashboardBody(
## Create tabs
tabItems(
# mapping
tabItem('my_map1',
fluidPage(
# Application title
titlePanel("Here is map 1"),
# Top panel with county name
verticalLayout(
wellPanel(textOutput("zip")),
# the map itself
mainPanel(
leafletOutput("map1", width = 700, height = 700)
)
)
)
),
tabItem("my_map2",
fluidPage(
# Application title
titlePanel("Here is map 2"),
# Top panel with county name
verticalLayout(
wellPanel(textOutput("zip")),
# the map itself
mainPanel(
leafletOutput("map2", width = 700, height = 700)
)
)
)
)
)
)
)
server <- function(input, output, session) {
output$map1 <- renderLeaflet(
leaflet() %>% addCircleMarkers(
lng = runif(10),
lat = runif(10),
layerId = paste0("marker", 1:10)))
observeEvent(input$map1_marker_click, {
leafletProxy("map1", session) %>%
removeMarker(input$map1_marker_click$id)
})
output$map2 <- renderLeaflet(
leaflet() %>% addCircleMarkers(
lng = runif(20),
lat = runif(20),
layerId = paste0("marker", 1:20)))
observeEvent(input$map2_marker_click, {
leafletProxy("map2", session) %>%
removeMarker(input$map2_marker_click$id)
})
}
shinyApp(ui, server)
1条答案
按热度按时间tvmytwxo1#
这个问题是由重复的
wellPanel(textOutput("zip"))
引起的-只需将渲染函数的结果分配给两个不同的输出即可避免: