闪亮-使用appendTab时折叠边栏

时间:2019-02-06 05:33:00

标签: r shiny shinyjs

单击示例中的标记之一后,我尝试在新选项卡中折叠边栏。侧栏应保留在“主仪表板”选项卡中

带有数据的小示例应用

library(shiny)
library(leaflet)
library(shinydashboard)
library(purrr)
library(shinyjs)

pts <- data.frame(
  id= letters[seq( from = 1, to = 10 )],
  x = rnorm(10, mean = -93.625), 
  y = rnorm(10, mean = 42.0285))

ui部分,其中应隐藏的边栏元素为id="Sidebar"useShinyjs()

ui <- dashboardPage(      
  dashboardHeader(title = "Test"),
  dashboardSidebar(
    sidebarMenu(id="Sidebar",
                actionLink("remove", "Remove detail tabs"))),
  dashboardBody(
    useShinyjs(), # not really sure where this has to go?
    tabsetPanel(
    id = "tabs",
    tabPanel(
      title = "Main Dashboard",
      value = "page1",
      fluidRow(tabsetPanel(id='my_tabsetPanel',
                               tabPanel('Map1',
                                        leafletOutput('map1')   
                               )))))))

我在Eventobserver()内添加appendTab的服务器部分,该部分应识别出已打开新标签并隐藏侧边栏

###server   
server <- function(input, output, session) {
  tab_list <- NULL

  output$map1 <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      setView(-93.65, 42.0285, zoom = 6)
  })
  observe({        
    input$my_tabsetPanel        
    tab1 <- leafletProxy('map1', data = pts) %>%
      clearMarkers() %>% 
      addCircleMarkers(lng = ~x, lat = ~y, radius = 4, layerId = ~id)})

  observeEvent(input$map1_marker_click, { 
    clickedMarker <- input$map1_marker_click[1]
    tab_title <- paste(clickedMarker)   
    appendTab(inputId = "my_tabsetPanel",
              tabPanel(
                tab_title,
                fluidRow(                      
                  box('test')
                )))
    tab_list <<- c(tab_list, tab_title) 

    observeEvent(input$tabs == "my_tabsetPanel", { #also not sure if this is at the right place?
        shinyjs::hide(id = "Sidebar")
      })
    updateTabsetPanel(session, "my_tabsetPanel", selected = tab_title)       
  })

  observeEvent(input$remove,{
    tab_list %>%
      walk(~removeTab("my_tabsetPanel", .x))
    tab_list <<- NULL
  })}  

shinyApp(ui = ui, server = server)

链接到similar but different problem

0 个答案:

没有答案