Nooga
Nooga

Reputation: 55

How to present an output without waiting for the rest of the script to run in R Shiny

I have a Shiny app that should calculate a value, present it and then use the same value for further more expensive computation. The problem is that it shows me the output only after it finishes evaluating the whole script. Here is a simple example:

library(shiny)
ui <- fluidPage(
  titlePanel("test"),
  sidebarLayout(
    sidebarPanel(
      textInput("text_in","Enter text here",value = "This is text to process"),
      actionButton("go", "Go")
    ),

    mainPanel(
      textOutput("first_text"),
      textOutput("results")
    )
  )
)

# Define server logic 
server <- function(input, output) {

num_letter<-eventReactive(input$go, {
  nchar(input$text_in)})
output$first_text <- renderText(num_letter())

sec_calculation<-eventReactive(num_letter(), {
  Sys.sleep(3)
  num_letter()*num_letter()})
output$first_text <- renderText(num_letter())
output$results <- renderText(sec_calculation())


 }
    # Run the application 
    shinyApp(ui = ui, server = server)

I added the Sys.sleep so it will be easier to see the problem. I would like to get the first output without waiting for the second one.

Upvotes: 1

Views: 133

Answers (1)

DeanAttali
DeanAttali

Reputation: 26323

This is not currently possible (at least not with native shiny code - you can always hack a workaround). An open issue for this exists on the shiny github repository: https://github.com/rstudio/shiny/issues/1705

Upvotes: 1

Related Questions