首页 > 解决方案 > 根据闪亮应用程序中的参数1值隐藏/显示参数2

问题描述

我是闪亮的应用程序/R 的新手,需要您的帮助来解决我的以下情况。

ui <- fluidPage(
  selectInput(inputId = "para1",label = "parameter1", choices = c("var1","Var2")),
  selectInput(inputId = "para2",label = "parameter2", choices = c("cars","trucks"))
)
server <- function(input,output,session){
  output$text <- renderText("control parameters")
}

shinyApp(ui,server)

我的要求是根据选择“parameter1”值显示/隐藏“parameter2”。
假设如果我选择parameter1=="var1",则显示“parameter2”,否则隐藏整个“parameter2”。

标签: rshiny

解决方案


您可以shinyjs::toggle结合使用uiOutput

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  selectInput(inputId = "para1",label = "parameter1", choices = c("var1","var2")),
  uiOutput("selectPara2")
)

server <- function(input,output,session){
  
  output$selectPara2 <- renderUI(selectInput(inputId = "para2",label = "parameter2", choices = c("cars","trucks"))) 
  
  shiny::observeEvent(input$para1, {
    shinyjs::toggle("selectPara2", condition = input$para1 == "var1")
  })
  
}

shinyApp(ui,server)

请注意,useShinyjs()需要在其中调用UI才能使其正常工作。


推荐阅读