Aleksandr Ianevski
Aleksandr Ianevski

Reputation: 1954

Several instances of system application in R shiny app

In the R shiny app, which is deployed on my server and works quite well, I have a button called "generate report". When you click on this button it calls 'pdflatex' through system() command to generate pdf report. system(paste0('pdflatex ', '-output-directory ./ ', texfile))

I may run several instances of my app and it works fine, however, if I click on the "generate report" button in all of the running instances at the same time, server can't generate reports and hangs, because all of the running shiny app instances call the same 'pdflatex' app installed on my server(Linux x64).

I am wondering about possibility to run one instance of system application (pdflatex) for one instance of shiny app.

Upvotes: 1

Views: 559

Answers (1)

Bogdan Rau
Bogdan Rau

Reputation: 625

You can. I had the same issue and my work around was to create a temporary folder for each session (keep in mind you'll need to enable session tracking:

shinyServer(function(input, output, **session**) {

})

The thought process is:

a. A user connects to your shiny app and opens up a session.

b. Based on that session, create a unique folder (perhaps in the www or a separate folder). You can generate a folder name as a random string using something like:

makeRandomString <- function(n = 1, length = 12) {
  randomString <- c(1:n)
  for (i in 1:n) {
    randomString[i] <- paste(sample(c(0:9, letters, LETTERS),
                                    length, replace = TRUE),
                             collapse = "")
  }

  return(randomString)
}

c. Save the pdf file there.

d. Have the user download the file from their own unique folder.

Keep in mind that if you are running open source shiny, you'll likely run into problems with multiple users clicking the generate report button at the same time as it runs single threaded, so people will be queued up.

Upvotes: 1

Related Questions