Shiny performing long-running calculations


I refer to the above link, which I think is what I need to solve the issue...
My original code is below:
output$RowsRuns <- renderUI({
    section <- as.integer(input$section)
    date_start <- as.character(input$FromDate)
    date_end <- as.character(input$ToDate)
  
    RawDataSetOutputs <- get_raw_dataset(section,date_start,date_end)) 
    RowsRuns <- paste0('Rows of Run: ',RawDataSetOutputs[[1]])
   HTML(RowsRuns)  
})

which the long-running computation intensive calculations is from the function 'get_raw_dataset' which took about more than 2 minutes to execute. It is able to display its output 'RowsRuns' after more than 2 minutes, and will affect other part of the Shiny application afterwards.

Hence, I start to look into ' Scaling Shiny with async' and my new code is now:

library(promises)
library(future)
plan(multisession)

r <- reactive({
  section <- as.integer(input$section)
  date_start <- as.character(input$FromDate)
  date_end <- as.character(input$ToDate)
  
  future(get_raw_dataset(section,date_start,date_end)) %...>% RawDataSetOutputs
})

output$RowsRuns <- renderUI({
  r() %...>% {
    RowsRuns <- paste0('Rows of Run: ',RawDataSetOutputs[[1]])
    HTML(RowsRuns)
  }
})

However, with the above new code, now the application is smooth and does not 'hang', but its output does not display. Did I do my code correctly when using 'future'?

Thanks.

1 Like

I think the mistake is in keeping RawDataSetOutputs which had meaning before, but not so much now.

library(promises)
library(future)
library(tidyverse)
library(shiny)
plan(multisession)

library(shiny)

ui <- fluidPage(
  sliderInput("section",
              label = "a slider",
              min = 0,
              max =10,
              value=5,
              step = 1),
  shiny::selectInput("what_to_show",
                     label="which part of the return to show?",
                     choices=c(1,2,3),
                     selected=1),
  uiOutput("RowsRuns")
)

myfunction <- function(x){
  list(x,x^2,paste0("hello ",x))
}

server <- function(input, output, session) {
  r <- reactive({
    section <- as.integer(input$section)
    # date_start <- as.character(input$FromDate)
    # date_end <- as.character(input$ToDate)
    
    future(myfunction(section))
  })
  
  output$RowsRuns <- renderUI({
    r() %...>% {
      RowsRuns <- paste0('Rows of Run: ',.[[as.integer(input$what_to_show)]])
      HTML(RowsRuns)
    }
  })
}

shinyApp(ui, server)

oic..do you mean that

[[as.integer(input$what_to_show)]]

is a input to show which output of the function we want from the list?
So for my case if I want to get the first output from the list, do I do this below?

library(promises)
library(future)
plan(multisession)

myfunction <- get_raw_dataset

r <- reactive({
  section <- as.integer(input$section)
  date_start <- as.character(input$FromDate)
  date_end <- as.character(input$ToDate)  
  future(myfunction(section,date_start,date_end)) 
})

output$RowsRuns <- renderUI({
  r() %...>% {
    RowsRuns <- paste0('Rows of Run: ',.[[1]])
    HTML(RowsRuns)
  }
})

Above still does not produce any output.

> myfunction <- get_raw_dataset
> myfunction(5001,'2020-01-01','2020-11-01')
[[1]]
[1] 63

I want to display the value of 63 for my case.

I provided a fully working example. If you need specific help with your code, then I would need a reprex from you.

Am I able to upload .RData file here?

output$RowsRuns <- renderUI({
    r() %...>% {
      RowsRuns <- paste0('Rows of Run: ',123)
      HTML(RowsRuns)
    }
  })

even I tried above, it does not show any output... isn't it supposed to ouput a string to display, afrer it has executed the reactive function?

I dont feel i should comment as you don't post runnable reprexes, you post partial snippets. ..

If you make the changes you have into the reprex I made, does it work?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

can you elaborate? which part of my code is wrong? how to make it work for my case?

library(promises)
library(future)
library(tidyverse)
library(shiny)
plan(multisession)

library(shiny)

ui <- fluidPage(
  sliderInput("section",
              label = "a slider",
              min = 0,
              max =10,
              value=5,
              step = 1),
  uiOutput("RowsRuns")
)

myfunction <- identity

server <- function(input, output, session) {
  r <- reactive({
    section <- as.integer(input$section)
    # date_start <- as.character(input$FromDate)
    # date_end <- as.character(input$ToDate)
    
    future(myfunction(section))
  })
  
  output$RowsRuns <- renderUI({
    r() %...>% {
      RowsRuns <- paste0('Rows of Run: ',.)
      HTML(RowsRuns)
    }
  })
}

shinyApp(ui, server)

I could only produce the output as below:

RawDataSetOutputs <- get_raw_dataset(5001,'2020-01-01','2020-11-01') 
> RawDataSetOutputs[[1]]
[1] 63

getting into the details of the function get_raw_dataset is too complex...

thanks for the code... can I find out if that 'identity' is supposed to be replaced with my 'get_raw_dataset'?
I have below:

myfunction <- get_raw_dataset

r <- reactive({
  section <- as.integer(input$section)
  date_start <- as.character(input$FromDate)
  date_end <- as.character(input$ToDate)
  
  future(myfunction(section,date_start,date_end)) 
})

output$RowsRuns <- renderUI({
  r() %...>% {
    RowsRuns <- paste0('Rows of Run: ',.)
    # RowsRuns <- paste0('Rows of Run: ',RawDataSetOutputs[[1]])
    HTML(RowsRuns)
  }
})

Above is not displaying any output text.
Is this line of code

  RowsRuns <- paste0('Rows of Run: ',.)

supposed to show its output?
take note here the output from the function is a list.