r 在Shiny中隐藏和显示元素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r 在Shiny中隐藏和显示元素相关的知识,希望对你有一定的参考价值。

# hide and show tab panels

library(shiny)

ui = fluidPage(
  checkboxInput("show", "Show data tab", value = FALSE),
  hr(),
  tabsetPanel(id = "my_tabset",
    tabPanel(
      "Plot",
      plotOutput("my_plot")
    ),
    tabPanel(
      "Data",
      dataTableOutput("my_data")
    )
  )
)

server = function(input, output) {
  output$my_plot <- renderPlot({
    mtcars %>% ggplot() + geom_point(aes(x = mpg, y = drat, color = cyl))
  })
  output$my_data <- renderDataTable({
    mtcars
  })
  observeEvent(input$show, {
    if (input$show) {
      showTab("my_tabset", "Data", select = TRUE)
    } else {
      hideTab("my_tabset", "Data")
    }
  })
}

shinyApp(ui, server)
# hide and show elements using if expression directly inside render*() function

library(shiny)

ui = basicPage(
  checkboxInput("show", "Show datatable",
                value = FALSE),
  dataTableOutput("mtcars")
)

server = function(input, output) {
  output$mtcars <- renderDataTable({
    if (input$show) {
      mtcars
    }
  })
}

shinyApp(ui, server)
# hide and show elements using conditional panel

library(shiny)

ui = basicPage(
  checkboxInput("show", "Show datatable",
                value = FALSE),
  conditionalPanel("input.show == true",
                   dataTableOutput("mtcars"))
)

server = function(input, output) {
  output$mtcars <- renderDataTable({
    mtcars
  })
}

shinyApp(ui, server)

以上是关于r 在Shiny中隐藏和显示元素的主要内容,如果未能解决你的问题,请参考以下文章