在闪亮的应用程序中隐藏和显示基于两个 actionButtons 的情节

时间:2021-04-07 14:45:05

标签: r shiny

我有一个闪亮的应用程序,它最初应该是空白的。当我按 "Show plot" 时,应该显示图,当我按 "Hide Plot" 时,应该隐藏图。

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      actionButton("showplot",
                   "Show plot"),
      actionButton("hideplot",
                   "Hide plot")
    ),
    mainPanel(
      plotOutput(outputId = "car_plot")
      
    )
  )
)

server <- function(input, output) {
  hidePlot <- reactiveVal(FALSE)
  showPlot <- reactiveVal(TRUE)
  
  
  
  observeEvent(input$hideplot, {
    hidePlot(TRUE) 
  })
  observeEvent(input$showplot, {
    showPlot(TRUE) 
  })
  
  output$car_plot <- renderPlot({
    if (hidePlot()){
      return(NULL)
    }
    else if (showPlot()){
      plot(cars)
    }
      
  })
}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

您只能使用hidePlot

  observeEvent(input$hideplot, {
    hidePlot(TRUE) 
  })
  observeEvent(input$showplot, {
    hidePlot(FALSE) 
  })
  
  output$car_plot <- renderPlot({
    if (hidePlot()){
      return(NULL)
    }
    else {
      plot(cars)
    }
  })

也许还有其他方式:

  observeEvent(input$showplot, {
    showPlot(TRUE) 
  })
  observeEvent(input$hideplot, {
    showPlot(FALSE) 
  })
  
  output$car_plot <- renderPlot({
    req(showPlot())
    plot(cars)
  })