0

I have shiny app contains multiple dynamic/conditional inputs (next input based on what I choose at previous one). For this purpose I use renderUI.

output$input1_server <- renderUI({ 
validate(need(base(), "Error"))  
 
df <- base %>%
      filter(age >= 10) %>%
      select(city)

selectInput(session$ns("input1"), "Input #1:", 
            choices = df$city, 
            width = "100%")
})

I also have reactive which uses inputs as arguments for filtering, such as:

data <- reactive({

base %>%
#some filters %>%
#some manipulations %>%
#etc 

})

Then I use reactive data() to build a reactive plot

plot <- reactive({

data() %>%
ggplot( ... ) %>%
geom_bar( ... ) %>%
#etc...

})

# Rendering
output$plot_output <- renderPlot({ plot() })

The problem occurs when I change dynamic input, which causes changing of other dynamic inputs. Then error in renderPlot() occurs, however, despite the error, in a few seconds plot appears. I am sure that error occurs because of vast number of renderUI. I tried to use sys.sleep to give more time for function executions, but it does not work. The code is correct, because when I use it out of shiny app it works.

jeparoff
  • 166
  • 8
  • 1
    Please make a small (minimal) example including sample data that reproduces the error. – HubertL Aug 26 '20 at 20:09
  • Please provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), i.e. a complete shiny app that shows this behaviour. Otherwise I'm not sure what your question is. But yes, in general a lot of `renderUI` can slow down your app – starja Aug 26 '20 at 20:09

1 Answers1

2

In the absence of minimal reproducible example, I'll give me best guess.
You could use req to make sure that data() and plot() are available :

plot <- reactive({
req(data())
data() %>%
ggplot( ... ) %>%
geom_bar( ... ) %>%
#etc...

})

# Rendering
output$plot_output <- renderPlot({ 
  req(plot)
  plot() })

Another option is validate:

plot <- reactive({
validate(need(nrow(data())>0,"Data not yet calculated"))
data() %>%
ggplot( ... ) %>%
geom_bar( ... ) %>%
#etc...

})

I hope this helps, but without MRE difficult to tell more.

Waldi
  • 39,242
  • 6
  • 30
  • 78