Search code examples
rshinyrscriptrshiny

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


I'm making R shiny app to generate data frames based on choosing. Only after selecting the data frame emp.data_B I want to present a header text like "Summary." How to do this?

Below is my code:

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)

Solution

  • You can create a uiOutput and show it conditionally.

    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" ))
        })
        
      }
    )