【问题标题】:re-hide conditional shiny output once it has been rendered渲染后重新隐藏条件闪亮输出
【发布时间】:2019-12-04 15:51:23
【问题描述】:

我需要一些关于如何在渲染后重新隐藏闪亮的输出的帮助。下面我提供了一个可重复的示例来解释我的问题。

我希望仅在选择选项 1 和 B 时显示文本 2.2,并且仅在选择选项 2 时显示文本 1。我已经通过在相应设置的条件中包含 conditionalPanel() 来做到这一点。

这可行,但是,一旦文本被渲染,当输入改变时,该文本不会消失。如果用户随后更改输入以选择任何其他选项,即选择选项 2,我希望文本 2.2 消失。

有没有可能用闪亮做到这一点?抱歉,如果以前有人问过这个问题 - 我无法通过搜索找到任何东西 - 非常感谢您的帮助!

library(shiny)

ui <- fluidPage(
  sidebarPanel(
    selectInput("Input1", label = "Input1", choices = c("Option 1", "Option 2") ),
    conditionalPanel(condition = "input.Input1 == 'Option 1'",
                     selectInput("Input2", label = "Input2",
                                 choices = c("A", "B"))),
  ),

  mainPanel(
    tabsetPanel(
      tabPanel("Tab 1", textOutput(outputId = "text1")),

      tabPanel("Tab 2", textOutput(outputId = "text2.1"), textOutput(outputId = "text2.2") )
    )
  )


)

server <- function(input, output) {

  observe({if(input$Input1 == 'Option 2'){
    output$text1 <- renderText("This text only shows for option 2")
  }})


  output$text2.1 <- renderText("some text")

  observe({if(input$Input2 == 'B'){
    output$text2.2 <- renderText("Show this only if option 1B is selected")
  }})


} 

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您需要在observe 环境中指定不同的if 可能性。这是一个解决方案:

    library(shiny)
    
    ui <- fluidPage(
      sidebarPanel(
        selectInput("Input1", label = "Input1", choices = c("Option 1", "Option 2") ),
        conditionalPanel(condition = "input.Input1 == 'Option 1'",
                         selectInput("Input2", label = "Input2",
                                     choices = c("A", "B"))),
      ),
    
      mainPanel(
        tabsetPanel(
          tabPanel("Tab 1", textOutput(outputId = "text1")),
    
          tabPanel("Tab 2", textOutput(outputId = "text2.1"), textOutput(outputId = "text2.2") )
        )
      )
    
    
    )
    
    server <- function(input, output) {
    
      observe({
        if(input$Input1 == 'Option 2'){
          output$text1 <- renderText("This text only shows for option 2")
          }
        else {
          output$text1 <- renderText("")
        }
        })
    
    
      output$text2.1 <- renderText("some text")
    
      observe({
        if (input$Input2 == "B") {
          output$text2.2 <- renderText("Show this only if option 1B is selected")
        }
        else {
          output$text2.2 <- renderText("")
        }
      })
    
    
    } 
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      相关资源
      最近更新 更多