【问题标题】:Hide and display plot based on one actionButton in shiny app在闪亮的应用程序中基于一个动作按钮隐藏和显示绘图
【发布时间】:2021-07-13 03:24:11
【问题描述】:

我有shiny 应用程序,默认情况下它会显示一个绘图。当我单击actionButton() 时,它会隐藏它,但我想再次单击相同的actionButton() 并显示它等等。

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      
      
      actionButton("hideshow_plot",
                   "HideShow plot")
    ),
    mainPanel(
      plotOutput(outputId = "car_plot")
      
    )
  )
)

server <- function(input, output) {
  showPlot <- reactiveVal(TRUE)

 
  
  observeEvent(input$hideshow_plot, {
    showPlot(FALSE) 
  })
  
  output$car_plot <- renderPlot({
    if (showPlot()){
      plot(cars)
    }
    else{
      
    }
  })
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你可以的

      observeEvent(input$hideshow_plot, {
        showPlot(!showPlot()) 
      })
    

    在每次点击时交替 TRUE/FALSE。

    【讨论】:

      【解决方案2】:

      考虑使用shinyjs

      library(shiny)
      
      ui <- fluidPage(
          shinyjs::useShinyjs(),
          sidebarLayout(
              sidebarPanel(
                  actionButton("hideshow_plot",
                               "HideShow plot")
              ),
              mainPanel(
                  plotOutput(outputId = "car_plot")
      
              )
          )
      )
      
      server <- function(input, output) {
          observeEvent(input$hideshow_plot, {
              shinyjs::toggle("car_plot")
          })
      
          output$car_plot <- renderPlot({
              plot(cars)
          })
      }
      
      shiny::shinyApp(ui, server)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-28
        • 2021-08-06
        • 1970-01-01
        • 2020-03-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多