首页 > 解决方案 > shinydashboard 切换框 - 默认隐藏

问题描述

这是对上一个线程的跟进。答案提供了一个默认显示该框的选项,但是如何将其更改为默认隐藏?下面的代码混合了两个答案。

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)

标签: rshinyshinydashboard

解决方案


您可以将它包裹在另一个周围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)

在此处输入图像描述


推荐阅读