简体   繁体   中英

R Shiny: multiple renderPlot calls in one R markdown document

BACKGOUND: You can "ask" RStudio to generate an example R Markdown Shiny document, which contains this sample code:

## Inputs and Outputs

You can embed Shiny inputs and outputs in your document.
Outputs are automatically updated whenever inputs 
change. This demonstrates how a standard R plot can be 
made interactive by wrapping it in the Shiny 
`renderPlot` function. The `selectInput` and 
`sliderInput` functions create the input widgets used to
drive the plot.

```{r, echo=FALSE}
inputPanel(
  selectInput("n_breaks", label = "Number of bins:",
              choices = c(10, 20, 35, 50), selected = 20),

  sliderInput("bw_adjust", label = "Bandwidth adjustment:",
              min = 0.2, max = 2, value = 1, step = 0.2)
)

renderPlot({
  hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
       xlab = "Duration (minutes)", main = "Geyser eruption duration")

  dens <- density(faithful$eruptions, adjust = input$bw_adjust)
  lines(dens, col = "blue")
})
```

Note that this example does not make use of a folder containing ui.R and server.R.

PROBLEM: If you copy this multiple times, the first one works as expected, and the later ones get displayed as well, but do not react to changes in the input parameters.

QUESTION: How can you create an R Markdown document with multiple embedded plots like the above (without using external folders with ui.R and server.R), but ensuring that each one works interactively?

You must give different ids to your input elements, something like that :

First embedded shiny plot :

```{r}
inputPanel(
  selectInput("n_breaks", label = "Number of bins:",
              choices = c(10, 20, 35, 50), selected = 20),

  sliderInput("bw_adjust", label = "Bandwidth adjustment:",
              min = 0.2, max = 2, value = 1, step = 0.2)
)

renderPlot({
  hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
       xlab = "Duration (minutes)", main = "Geyser eruption duration")

  dens <- density(faithful$eruptions, adjust = input$bw_adjust)
  lines(dens, col = "blue")
})
```

Second embedded shiny plot :

```{r}
inputPanel(
  selectInput("n_breaks2", label = "Number of bins:",
              choices = c(10, 20, 35, 50), selected = 20),

  sliderInput("bw_adjust2", label = "Bandwidth adjustment:",
              min = 0.2, max = 2, value = 1, step = 0.2)
)

renderPlot({
  hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks2),
       xlab = "Duration (minutes)", main = "Geyser eruption duration")

  dens <- density(faithful$eruptions, adjust = input$bw_adjust2)
  lines(dens, col = "blue")
})
```

As described for example in the RStudio Shiny Tutorial , the first parameter to widget functions is the widget name, which identifies the widget. Multiple widgets with the same name will not each be usable, which is why simply creating two copies of the example does not create two working copies.

To make it work, you must make the widget names unique in each inputPanel call, and then use this names in the renderPlot calls.

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