I have a question regarding R shiny and more especially about the way to improve performance of my shiny app. I'm using some SQL queries that take quite long to run as well as some plot which take time to show up. I know that it is possible to use future
and promises
but I can't figure out how to use them in a reactive expressions.
I show you below a really simple example:
library(shiny)
library(ggplot2)
library(plotly)
library(dplyr)
ui <- fluidPage(
selectInput("id1", "Choose:", choices = unique(as.character(iris$Species))),
dataTableOutput("my_data"),
plotlyOutput("my_plot")
)
server <- function(input, output, session) {
output$my_data <- renderDataTable({
iris %>% filter(Species == input$id1)
})
data_to_plot <- reactive({
Sys.sleep(10)
res <- ggplot(iris %>% filter(Species == input$id1), aes(x=Sepal.Length)) + geom_histogram()
return(res)
})
output$my_plot <- renderPlotly({
ggplotly(data_to_plot())
})
}
shinyApp(ui, server)
In this case, we can see that the app waits for the plot part to end computation before showing the whole app. However I'd like the datatable to show even if the part with the plot hasn't finish its computation.
Thanks a lot in advance for your help !
Officially shiny doesn't support intra-session non-blocking promises.
Please read this carefully.
Here is how to apply the above workaround to your code: