When I have reactive data that gets passed to renderPlot
(or other rendering functions), the data is often initially empty until some action happens. The default rendering typically displays an error in the app before the action happens because the data is empty, ie
Error 'x' must be numeric
in this example. Is there some standard way to have the rendering functions act when they don't have data (maybe not rendering if there is an error or just being blank)? I know I could go to the trouble of structuring all the reactive values so the output would be blank, but it seems like unnecessary work.
Example in rMarkdown shiny
---
title: "Example"
runtime: shiny
output: html_document
---
```{r}
shinyApp(
shinyUI(fluidPage(
inputPanel(
numericInput("n", "n", 10),
actionButton("update", "Update")
),
plotOutput("plot")
)),
shinyServer(function(input, output) {
values <- reactiveValues()
values$data <- c()
obs <- observe({
input$update
isolate({ values$data <- c(values$data, runif(as.numeric(input$n), -10, 10)) })
}, suspended=TRUE)
obs2 <- observe({
if (input$update > 0) obs$resume()
})
output$plot <- renderPlot({
dat <- values$data
hist(dat)
})
})
)
```
You can use the
exists
function to see if a variable exists before trying to plot it, and change it as desired otherwise: