gpt4 book ai didi

r - 在R Shiny中显示/隐藏整个框元素

转载 作者:行者123 更新时间:2023-12-01 07:04:06 25 4
gpt4 key购买 nike

我目前正在尝试寻找一种方法来隐藏/显示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也许有可能,但我似乎无法理解它应如何与当前结构一起使用。

最佳答案

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

它使用shinyjs显示/隐藏框。关键是给该框一个id,然后在id/show函数中使用该hide

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)

关于r - 在R Shiny中显示/隐藏整个框元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44790028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com