显示/隐藏R闪亮中的整个框元素 [英] Show/hide entire box element in R Shiny
本文介绍了显示/隐藏R闪亮中的整个框元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试找到一种方法来隐藏/显示整个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)
主要思想是拥有属于每个框的一个按钮(或多个按钮),并仅隐藏/显示该特定框。也许用shinyjs可以做到这一点,但我似乎不能理解它应该如何与我目前的结构一起工作。
推荐答案
下面是一个可以扩展到实际应用程序中的最小示例。
它使用shinyjs
显示/隐藏该框。关键是给该框一个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)
这篇关于显示/隐藏R闪亮中的整个框元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文