[英]Show/hide entire box element in R Shiny
I'm currently trying to find a way to hide/show an entire box() element (and everything inside) in R Shiny. 我目前正试图找到一种方法来隐藏/显示R Shiny中的整个box()元素(以及里面的所有内容)。 I want to create a maybe a button which allows the user to expand a specifict box and then to hide it with the same (or even different) button. 我想创建一个可能是一个按钮,允许用户扩展一个特定的框,然后用相同(甚至不同)的按钮隐藏它。 I do not want to use conditionalPanel, as my application is really big and it creates some problems. 我不想使用conditionalPanel,因为我的应用程序非常大并且会产生一些问题。
A sample code can be found below: 示例代码可以在下面找到:
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)
The main idea is to have a button(or buttons) that belong to each box and hide/show only that specific box. 主要思想是有一个按钮(或按钮)属于每个框,并隐藏/显示该特定框。 Maybe it's possible with shinyjs, but I can't seem to comprehend how it should work with my current structure. 也许有可能使用shinyjs,但我似乎无法理解它应该如何与我目前的结构一起工作。
Here's a minimal example that you can extend into your actual application. 这是一个可以扩展到实际应用程序的最小示例。
It uses shinyjs
to show/hide the box. 它使用shinyjs
来显示/隐藏盒子。 The key is to give the box an id
, and then use that id
value in the show
/ hide
functions 关键是给框一个id
,然后在show
/ hide
函数中使用该id
值
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, {
if(input$button %% 2 == 1){
shinyjs::hide(id = "myBox")
}else{
shinyjs::show(id = "myBox")
}
})
}
shinyApp(ui, server)
Instead of checking if its divisible by 2 why not use toggle
functionality within shinyjs
而不是检查它是否可被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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.