Nick
Nick

Reputation: 3374

Plotting with action button in Shiny Rmarkdown

I am curious as to why the following does not work. When trying to plot using a reaction to an action button, it fails to show the plot in Shiny.

Any idea what I am doing wrong? I have tried many iterations and ways of doing this. Any ideas if Shiny can generate a plot when called in a reactive environment?

---
title: "Will not plot"

output: html_document

runtime: shiny

---


```{r setup, include=FALSE}
 knitr::opts_chunk$set(echo = TRUE)
```

```{r, echo = FALSE}

 inputPanel(
  actionButton("Plot", "Save and Open TRI"),
  numericInput("Size", label = "Size", value = 1000, 1000, 5000, 1000)
              )
# Works:
renderPlot({
plot(runif(1000)
     )})

# Doesn't work:
eventReactive(input$Plot,
{
  renderPlot({
plot(runif(1000)
 )})
}
)

observeEvent(input$Plot,
{
  renderPlot({
    plot(runif(1000)
     )})
}
)

```

Upvotes: 7

Views: 5078

Answers (1)

tospig
tospig

Reputation: 8333

This should do it

---
title: "Will not plot"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)  
```


```{r, echo = FALSE}

 inputPanel(
  actionButton("Plot", "Save and Open TRI"),
  numericInput("Size", label = "Size", value = 1000, 1000, 5000, 1000)
              )

p <- eventReactive(input$Plot, {plot(runif(1000))})

renderPlot({
  p()
  })

```

Reference: Shiny Action Buttons - see pattern 2 - eventReactive in particular.

Upvotes: 8

Related Questions