Show/hide R Shiny 中的整个框元素

Show/hide entire box element in R Shiny

我目前正在尝试找到一种方法来 hide/show R Shiny 中的整个 box() 元素(以及里面的所有东西)。我想创建一个可能是一个按钮,它允许用户展开一个特定的框,然后使用相同(甚至不同)的按钮将其隐藏。我不想使用 conditionalPanel,因为我的应用程序非常大并且会产生一些问题。

示例代码如下:

library(shiny)
library(shinydashboard)
library(collapsibleTree)
require(colorspace)
# Dataset is from https://community.tableau.com/docs/DOC-1236
load(system.file("extdata/Superstore_Sales.rda", package = "collapsibleTree"))
# For the sake of speed, let's only plot sales in Ontario
Superstore_Sales <- Superstore_Sales[Superstore_Sales$Region=="Ontario",]

# Define UI for application that draws a collapsible tree
ui <- fluidPage(

  # Application title
  titlePanel("Collapsible Tree Example 3: Gradient Mapping"),

  # Sidebar with a select input for the root node
  sidebarLayout(
    sidebarPanel(

      tags$a(href = "https://community.tableau.com/docs/DOC-1236", "Sample dataset from Tableau")
    ),

    # Show a tree diagram with the selected root node
    mainPanel(
      box(title="Tree Output",width='800px',
        collapsibleTreeOutput("plot", height = "500px")
      ),
      box(title="Input",
        selectInput(
          "hierarchy", "Tree hierarchy",
          choices = c(
            "Customer Segment", "Product Category", "Product Sub-Category",
            "Order Priority", "Product Container"
          ),
          selected = c("Customer Segment","Product Category", "Product Sub-Category"),
          multiple = TRUE
        ),
        selectInput(
          "fill", "Node color",
          choices = c("Order Quantity", "Sales", "Unit Price"),
          selected = "Sales"
        )


      )  
    )
  )
)

# Define server logic required to draw a collapsible tree diagram
server <- function(input, output) {
  output$plot <- renderCollapsibleTree({
    collapsibleTreeSummary(
      Superstore_Sales,
      hierarchy = input$hierarchy,
      root = input$fill,
      attribute = input$fill
    )
  })
}

# Run the application
shinyApp(ui = ui, server = server)

主要思想是让一个(或多个)按钮属于每个框,并且 hide/show 只属于那个特定的框。也许 shinyjs 是可能的,但我似乎无法理解它应该如何与我当前的结构一起工作。

这是一个您可以扩展到实际应用程序中的最小示例。

它使用shinyjs到show/hide的框。关键是给盒子一个 id,然后在 show/hide 函数中使用那个 id

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

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            useShinyjs()    ## IMPORTANT: so hiny knows to use the shinyjs library
        ),
        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, {
        
        if(input$button %% 2 == 1){
            shinyjs::hide(id = "myBox")
        }else{
            shinyjs::show(id = "myBox")
        }
    })
}

shinyApp(ui, server)

而不是检查它是否可以被 2 整除为什么不在 shinyjs

中使用 toggle 功能
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)