Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting with action button in Shiny Rmarkdown

Tags:

r

shiny

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)
     )})
}
)

```
like image 291
Nick Avatar asked Dec 18 '15 08:12

Nick


1 Answers

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.

like image 134
tospig Avatar answered Oct 11 '22 10:10

tospig