Shinydashboard切换框-默认隐藏

时间:2020-06-18 14:11:24

标签: r shiny shinydashboard

这是对a previous thread的跟进。答案提供了一个默认情况下显示该框的选项,但是如何更改为默认隐藏呢? 下面的代码混合了两个答案。

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      useShinyjs()
    ),
    mainPanel(
      box(id = "myBox", title = "Tree Output", width = '800px',
          selectInput(inputId = "myInput", label = "my input", choices = c(letters))
      ),
      actionButton(inputId = "button", label = "show / hide")
    )
  )
)

server <- function(input, output){

  ## observe the button being pressed
  observeEvent(input$button, {
    shinyjs::toggle("myBox")
  })
}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

您可以将其包装在另一个div周围,并使用hidden中的shinyjs函数

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            useShinyjs()
        ),
        mainPanel(
            hidden(
                div(id = "mybox_wrapper",
                    box(id = "myBox", title = "Tree Output", width = '800px',
                        selectInput(inputId = "myInput", label = "my input", choices = c(letters))
                    )
                )),
            actionButton(inputId = "button", label = "show / hide")
        )
    )
)

server <- function(input, output){

    ## observe the button being pressed
    observeEvent(input$button, {
        shinyjs::toggle("mybox_wrapper")
    })
}

shinyApp(ui, server)

enter image description here

相关问题