1

我正在制作 R 闪亮的应用程序来根据选择生成数据框。只有在选择数据框 emp.data_B 之后,我才想显示一个标题文本,如“摘要”。这该怎么做?

下面是我的代码:

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)
4

1 回答 1

1

您可以创建一个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" ))
    })
    
  }
)
于 2021-05-27T04:45:40.380 回答