Files accessed by many users concurrently


I have an editable table in my program that saves the changes made to the source file and saves the file to the tmp directory. I am thinking that when I deploy the application to Shiny or SnihyIO, the file and directory are going to be accessed by many users concurrently. The solution I though of consists on naming the source file in a different manner according to the recognized user that makes and saves changes to the source file through the editable table. Also I would need to create a different directory per user. This is going to fill the sever directory with folders and files that we are going to have to remove often. I was wondering if there is a built in way of doing this in Shiny.

Thank you

I must say I never got the problem of concurrent user writing at the same time on a the same file. For one app, I used a database to store user specific information that they could edit. (using mongodb). Working with a database is often safer as they sometimes have concurrent mechanism built-in. (i.e Postgre concurrent control)

For a file, I never used that approach before, but maybe a filelock mechanism can help you know if it is safe or not to read or write to the file, and wait for your turn when filelock is active.
Their is a :package: for that:

If you write the files to R's built in temp dir, the files will be cleaned up upon process exit (unless the process is killed/crashes particularly harshly). You will need to have unique filenames/subdirectories though. session$token is one unique ID you could use.

1 Like

Hi Joe,

Thank you for your reply. Could you talk a little bit more about session$token or pointing me to the documents where I can read about it?


Thank you so much for your information.

1 Like

Here's an example of what I mean. (Set the prefix variable to a short string prefix that makes sense for your app, so if you see these files in your R tmp dir you can tell what they are.)


ui <- fluidPage(
  "This session's unique file is located at", textOutput("path", container = tags$code)

prefix <- "myapp-"

server <- function(input, output, session) {
  # Make a unique file for this session
  filepath <- tempfile(paste0(prefix, session$token), fileext = ".txt")
  output$path <- renderText(filepath)

shinyApp(ui, server)

Thanks very much for the example.