gpt4 book ai didi

r - 如何为特定选项卡隐藏 Shiny 应用程序的侧边栏面板

转载 作者:行者123 更新时间:2023-12-04 10:14:50 25 4
gpt4 key购买 nike

我的 Shiny 应用程序有一个通用的侧边栏面板。我想为一个特定的选项卡隐藏它,即每当用户导航到该选项卡时,sidebarPanel 就会折叠。我正在尝试的代码如下-

用户界面-

library(shiny)
shinyUI(fluidPage (
theme = shinytheme("superhero"),
headerPanel("COVID-19 Data Visualizer"),
sidebarPanel(
width = 2,
selectInput(
"countries",
label = "Select Countries",
choices =
c("B", "C", "A"),
selected = c("A"),
multiple = T
),
submitButton(text = "View")
),
mainPanel (h1(""),
tabsetPanel(
tabPanel(
"Global Status",
div(id="Main"),
plotlyOutput("figG"),
br(),
plotlyOutput("global_time"),
br(),
plotlyOutput("global_cfr"),
br(),
plotlyOutput("global_p"),
br(),
plotlyOutput("global_recov_dead")
),
tabPanel(
"Comparative Charts",
plotlyOutput("fig_confirm"),
br(),
plotlyOutput("fig_dead"),
br(),
plotlyOutput("fig_recov")
),
tabPanel(
"Ratio Analysis",
plotlyOutput("fig_confirm_S"),
br(),
plotlyOutput("fig_confirm_D"),
br(),
plotlyOutput("fig_Ratio"),
br(),
plotlyOutput("fig_cfr_print")
)
))
))

服务器部分-

server <- function(input, output) {
observeEvent(input$tabs == "Global Status", {
shinyjs::hide(id = "Main")
})

}

我真的不想使用 navbarPage,而是想为所有输入使用一个侧边栏面板。

我得到的输出截图- enter image description here

提前致谢。

最佳答案

这是一个例子:

library(shiny)
library(shinyjs)

ui <- fluidPage(
useShinyjs(),

sidebarLayout(

sidebarPanel(
id="sidebar",
selectInput(
"countries", label = "Select Countries",
choices = c("B", "C", "A"), selected = "A",
multiple = TRUE
)
),

mainPanel(
tabsetPanel(
tabPanel(
"Global status",
sliderInput("s1", "slider 1", 0, 100, 20)
),
tabPanel(
"Comparative charts",
sliderInput("s2", "slider 2", 0, 100, 50)
),
tabPanel(
"Ratio analysis",
sliderInput("s3", "slider 3", 0, 100, 80)
),
id = "tabset"
),
id="main"
)
)
)

server <- function(input, output){

observeEvent(input[["tabset"]], {
if(input[["tabset"]] == "Comparative charts"){
hideElement(selector = "#sidebar")
removeCssClass("main", "col-sm-8")
addCssClass("main", "col-sm-12")
}else{
showElement(selector = "#sidebar")
removeCssClass("main", "col-sm-12")
addCssClass("main", "col-sm-8")
}
})

}

shinyApp(ui = ui, server = server)

enter image description here

关于r - 如何为特定选项卡隐藏 Shiny 应用程序的侧边栏面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61120731/

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