单独选项卡中的多个瓣叶图未显示在R Shiny Jmeter 板中

gojuced7  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(110)

我尝试在单独的标签项中渲染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)
tvmytwxo

tvmytwxo1#

这个问题是由重复的wellPanel(textOutput("zip"))引起的-只需将渲染函数的结果分配给两个不同的输出即可避免:

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")), # <- issue
                  wellPanel(textOutput("zip1")),

                  # 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")), # <- issue
                  wellPanel(textOutput("zip2")),
                  
                  # 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)
  })
  
  output$zip2 <- output$zip1 <- renderText({"Something useful"})
  
}

shinyApp(ui, server)

相关问题