Show blank area in gloss instead of error

When I have reactive data that is being passed to renderPlot

(or other render functions), the data is often initially empty until some action takes place. By default, rendering usually displays an error in the application before the action occurs because the data is empty i.e.

Error 'x' must be numeric

in this example. Is there some standard way for the render functions to take effect when they have no data (maybe no rendering if there is an error or just empty)? I know I can solve the problem of structuring all reactive values, so the output will be empty, but that seems like unnecessary work.

Example at 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)
        })
    }) 
)
```

      

+3


source to share


1 answer


You can use a function exists

to see if a variable exists before trying to build it, and modify it otherwise as desired:



renderPlot({
    if(exists("values$data")) {
      dat <- values$data 
    } else {
      dat <- 0
    }
    hist(dat)
})

      

+1


source







All Articles