根据闪亮应用中的条件隐藏helptext()

时间:2018-07-04 22:55:51

标签: r shiny

我有一个简单的闪亮应用程序:

library(shiny)

ui <- navbarPage(
  "Application",
  tabPanel("General",
           sidebarLayout(

             sidebarPanel(

             ),

             mainPanel(
               wellPanel(
                 tags$style(type="text/css",
                            ".shiny-output-error { visibility: hidden; }",
                            ".shiny-output-error:before { visibility: hidden; }"
                 ),
                 uiOutput("c1"),
                 uiOutput("num8"),
                 uiOutput("help1")

               )



             )
           )

  )
)

server <- function(input, output,session) {

  output$c1<-renderUI({
    checkboxInput("ch1", 
                  h5("Person ID"), value = FALSE)
  })

  output$num8<-renderUI({
    if(input$ch1==T){
      textInput("nm8", 
                h6("Column"), 
                value = 1)
    }
    else{
      output$help1<-renderUI({
        helpText("Click Person ID")
      }) 
    }

  })
}
shinyApp(ui, server)

我想要实现的是单击复选框时隐藏帮助文本“ Click Person ID”。我为此使用了if条件,但是当数字输入必须隐藏在帮助文本中时,它总是存在的。

1 个答案:

答案 0 :(得分:1)

我看到renderUI()包裹在renderUI()中。那可能会惹上你的麻烦。您可以将它们分开,然后它将起作用。

  output$num8<-renderUI({
    if(input$ch1 == TRUE){
      textInput("nm8", 
                h6("Column"), 
                value = 1)
    }
  })

  output$help1<-renderUI({
    if(input$ch1 == FALSE){
      helpText("Click Person ID")
    } 
  })

完整代码将显示为:

library(shiny)
ui <- navbarPage(
  "Application",
  tabPanel("General",
           sidebarLayout(

             sidebarPanel(

             ),

             mainPanel(
               wellPanel(
                 tags$style(type="text/css",
                            ".shiny-output-error { visibility: hidden; }",
                            ".shiny-output-error:before { visibility: hidden; }"
                 ),
                 uiOutput("c1"),
                 uiOutput("num8"),
                 uiOutput("help1")

               )
             )
           )
  )
)

server <- function(input, output,session) {

  output$c1<-renderUI({
    checkboxInput("ch1", 
                  h5("Person ID"), value = FALSE)
  })

  output$num8<-renderUI({
    if(input$ch1 == TRUE){
      textInput("nm8", 
                h6("Column"), 
                value = 1)
    }
  })

  output$help1<-renderUI({
    if(input$ch1 == FALSE){
      helpText("Click Person ID")
    } 
  })


}
shinyApp(ui, server)