当我 select R shiny 中的数据帧 "emp.data B" 时,如何将 heading/tag 渲染为 "Summary"?

When I select the dataframe "emp.data B" in R shiny, how do I render a heading/tag as "Summary"?

我正在制作 R shiny 应用程序以根据选择生成数据帧。只有在选择数据框 emp.data_B 之后,我才想显示一个 header 文本,例如“摘要”。如何做到这一点?

下面是我的代码:

library(shiny)

emp.data_A <- data.frame(
  emp_id = c(1:5),
  emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
  salary = c(623.3,515.2,611.0,729.0,843.25))
  
emp.data_A


emp.data_B <- data.frame(
  emp_id = c(1:7),
  emp_name = c("Rick","Dan","Michelle","Ryan","Gary", "Vicky", "Armstrong"),
  salary = c(623.3,515.2,611.0,729.0,843.25, 845.90, 852.20))
emp.data_B


shinyApp(
  ui = tagList(
    navbarPage(
      selectInput("dataset5", "Choose a dataset:",
                  choices = c("SelectDataSet ", "emp.data_A", "emp.data_B")),
      # Button
      downloadButton("downloadData5", "Download")
    ),
    mainPanel(
      tableOutput("table5")
  )
),

server = function(input, output,session) {
  datasetInput <- reactive({
    switch(input$dataset5,
           "emp.data_A" = emp.data_A,
           "emp.data_B" = emp.data_B)
  })
  output$table5 <- renderTable({
    datasetInput()
  })

}
)
shinyApp(ui, server)

您可以创建一个 uiOutput 并有条件地显示它。

library(shiny)

shinyApp(
  ui = tagList(
    navbarPage(
      
      selectInput("dataset5", "Choose a dataset:",
                  choices = c("SelectDataSet ", "emp.data_A", "emp.data_B")),
      # Button
      downloadButton("downloadData5", "Download")
    ),
    mainPanel(
      uiOutput("header"),
      tableOutput("table5")
    )
  ),
  
  server = function(input, output,session) {
    datasetInput <- reactive({
      switch(input$dataset5,
             "emp.data_A" = emp.data_A,
             "emp.data_B" = emp.data_B)
    })
    output$table5 <- renderTable({
      datasetInput()
    })
    
    output$header <- renderUI({
      if(input$dataset5 == "emp.data_B") 
        tags$pre(h4("Summary Extraction \t \t VS \t \t Extraction Summary" ))
    })
    
  }
)