[英][RShiny][Shinydashboard]Show/Hide different input panel based on another input
I have built shiny App before, but I am getting rusty after not writing the code for a while.我以前构建过闪亮的应用程序,但是在一段时间不编写代码后我变得生疏了。
Here is my goal:这是我的目标:
I am designing a dynamic UI using shinydashboard.我正在使用 Shinydashboard 设计一个动态 UI。 I have one selectInput (eg, A, B, C and D).我有一个 selectInput(例如,A、B、C 和 D)。 What I want to make the app perform is that, based on the user option (either A, B, C or D), it will dynamically pop up another input panel (a panel with specific collection of "numericInput"s or "selectInput"s), as each A/B/C/D option defines different situation.我想让应用程序执行的是,根据用户选项(A、B、C 或 D),它会动态弹出另一个输入面板(具有特定“numericInput”或“selectInput”集合的面板) s),因为每个 A/B/C/D 选项定义了不同的情况。
I could not provide my own code (as I do not have any idea on even how to get started), but I find some page with example code here:我无法提供我自己的代码(因为我什至不知道如何开始),但我在这里找到了一些带有示例代码的页面:
An example of dynamic UI 动态 UI 示例
It is the example in section 10.2.1 of the page using tabsetPanel and updateTabsetPanel.它是使用 tabsetPanel 和 updateTabsetPanel 的页面第 10.2.1 节中的示例。
I checked their app link and it seems to be exactly what I want, but I suspect that some of the code has been depleted by Shiny already as when I copy and paste the code to my R script, I could not run it without error.我检查了他们的应用程序链接,它似乎正是我想要的,但我怀疑某些代码已经被 Shiny 耗尽,因为当我将代码复制并粘贴到我的 R 脚本时,我无法毫无错误地运行它。
If you could kindly help, either on how to properly adjust the code on that page, or if there is any other approach, it will be greatly appreciated.如果您能提供帮助,无论是如何正确调整该页面上的代码,或者如果有任何其他方法,我们将不胜感激。
Easiest way would be using uiOutput
and renderUI
.最简单的方法是使用uiOutput
和renderUI
。
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("controller", "Show", choices = c("A", "B", "C", "D")),
uiOutput("newSelect")
),
mainPanel(
textOutput("textId")
)
)
)
server <- function(input, output, session) {
output$textId <- renderText({
input$controller
})
output$newSelect <- renderUI({
if(input$controller == "B"){
selectInput("controller2", "New Select", choices = c(1:4))
} else {
NULL
}
})
}
shinyApp(ui,server)
Bare in mind this can make your app laggish if it gets too complex.请记住,如果它变得过于复杂,这可能会使您的应用程序滞后。 If so, you can use some js to show or hide the other selectInputs conditionally如果是这样,您可以使用一些js有条件地显示或隐藏其他selectInputs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.