简体   繁体   中英

How to delete a chart using observeEvent in shiny?

I am using the shiny runtime in flexdashboard.

I would like to hide a chart on button press ( input$updt ), and I was trying this:

   ---
title: "Students Data - College of Business"
output: 
  flexdashboard::flex_dashboard:
      orientation: columns 

runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(shinyjs)
shinyjs::useShinyjs(rmd=TRUE)
```

Column {data-width=650}
-----------------------------------------------------------------------

### Chart A

```{r}

actionButton('updt', 'Update chart')
output$p1 <-renderPlot({ ggplot(diamonds, aes(carat, price)) + geom_point() }, height=800)
plotOutput("p1")

output$p2 <- renderPlot({
   ggplot(mtcars, aes(cyl, disp))+ geom_point()
})

plotOutput("p2")
observeEvent(input$updt, {hide("p1")})


```

However, when I click on the button it doesn't hide p1

hide() needs the id of the element that contains the plot, but renderPlot doesn't allow setting the outputId in outputArgs .

A workaround is to use plotOutput :

library(shinyjs)
shinyjs::useShinyjs(rmd=TRUE)
actionButton('updt', 'Update chart')

output$p1 <- renderPlot({load('p1.RDA') p1}, height=800)
plotOutput("p1")
output$p2 <- renderPlot(expr={ [...code to recalculate p2]  })
plotOutput("p2")
observeEvent(input$updt, {hide("p1")})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM