R Shiny - Continuous Background Task

I would like to know if it is possible to maintain a continuous background job while the Shiny application is running. This means, for example, that a stream can load data from a web page into a database while shinyApp is running.

Moreover, could it be possible to interact with data from an external background β€œstream”? (for example, during loading, plot creation, or something else).

The video tutorial says that: "Code outside the server function will be run once per R (working) session." So, the code I need, I think, should be placed outside the server function.

Is it possible to reach the scene that I am describing? Or do I need another external instance of R (outside shinyApp scripts)?

Thanks in advance.

+6
source share
2 answers

I thought about this, and I think it is possible, but the implementation that I mean depends on the platform. In this case, I will consider ubuntu 14.04.

Suppose you have some kind of computationally intensive task:

ui.R:

library(shiny) fluidPage( numericInput('number','Number',10000000), textOutput('CalcOutput') ) 

server.R

 library(shiny) function(input,output,session) { output$CalcOutput <- renderText({ sort(runif(input$number)) }) } 

transfer the operation to the function of the corresponding variables in the subfile:

newfile.R

 saveRDS(sort(runif(commandArgs(TRUE)[1])), file = 'LargeComputationOutput') 

change your server. R

 function(input, output) { observe({ # Starts script as a background process, but completes instantaneously system(paste('Rscript newfile.R',input$number,'&')) }) CalculationOutput <- reactive({ invalidateLater(5000) validate( need(file.exists('LargeComputationOutput'),'Calculation In Progress'), need(file.info('LargeComputationOutput')$mtime > Sys.time()-5,'Calculation In Progress') ) x <- readRDS('LargeComputationOutput') }) output$CalcOutput <- renderText({ CalculationOutput()[300] }) } 

This is still a bit wrong, but it is a proof of concept that you can move intensive operations into subprocesses and detect a reactive listener when doing these calculations.

EDIT: Shiny will also need permissions to write to the appropriate location.

+1
source

I found a solution to this problem using a future package. Please see my answer in Calling a brilliant JavaScript callback from the future.

+1
source

All Articles