【发布时间】:2017-12-01 01:59:11
【问题描述】:
我目前正在尝试找到一种方法来隐藏/显示 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)
主要思想是拥有一个属于每个框的按钮(或多个按钮)并仅隐藏/显示该特定框。也许使用 shinyjs 是可能的,但我似乎无法理解它应该如何与我当前的结构一起使用。
【问题讨论】:
-
是标题为“树输出”和“输入”的两个框。我希望它们显示为隐藏并可以通过按钮展开。
-
我的意思是,它们来自哪个库?
-
哦,shinydashboard 是库 - 忘记将它添加到代码中。我现在就编辑它
-
为什么不在
shinydashboard::box中使用collapsible参数? -
可能是因为需要配合
dashboardPage、dashboardHeader等使用,没关系
标签: r shiny shinydashboard shinyjs