Michael Kozak
Michael Kozak

Reputation: 99

Delete output plots in shiny

I am trying to initialize output plots in shiny when button is clicked, meaning output plots will be deleted from the screen when button is pressed, but I don't know the exact command for that. I tried something like:

observedEvent(input$button, { output$plot1 <- NULL })

but it doesn't work. Hope you can help,

Thanks

Upvotes: 1

Views: 2175

Answers (1)

Pork Chop
Pork Chop

Reputation: 29387

Instead of deleting the plot, you can either show or hide it with shinyjs

rm(list=ls())
library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  sidebarPanel(actionButton("button", "Hide Plot1"),
               actionButton("button2", "Show Plot1"),br(),
               actionButton("button3", "Hide Plot2"),
               actionButton("button4", "Show Plot2")),
  mainPanel(plotOutput("plot1"),plotOutput("plot2"))
)

server <- function(input, output, session) {
  observeEvent(input$button, {
    hide("plot1")
  })
  observeEvent(input$button2, {
    show("plot1")
  })
  observeEvent(input$button3, {
    hide("plot2")
  })
  observeEvent(input$button4, {
    show("plot2")
  })

  output$plot1 <- renderPlot({
    hist(mtcars$mpg)
  })
  output$plot2 <- renderPlot({
    hist(mtcars$qsec)
  })

}
shinyApp(ui, server)

Upvotes: 3

Related Questions