How to create a working directory for each user in Shiny app?

I'm building a shiny app that create multiple files depending on the upload file by the user. The created files has the same names and this can make that when a user download the files end with a none relative information to his data.

How can be created an independent directory for each user?

I have found the next solution so far to create the directory to store the files:

directory <- paste0(format(Sys.time(),"%Y%m%d%H%M%S"),rnorm(1),
                    rnorm(1))
dir.create(directory)
setwd(directory)

But I have read that this may not work if I upload the app to a server.

This question can also be find at Stack overflow

Hi,

Setting the working directory is likely not going to work like that in Shiny. I suggest you generate a folder based off the user's session token, which is generated when a user connects to a Shiny app and located in the session variable.

library(shiny)

ui <- fluidPage(
  
)

server <- function(input, output, session) {
  
  dir.create(session$token)
  file.create(paste0(session$token, "/userFile.txt"))
  
}

shinyApp(ui, server)

Once you created the directory, you can save any file in that one using again the token which is the name of the base folder for that user. You should remember to erase the folder after the task finished, or you will have a lot of folders soon.

Why do you need to store user specific files anyway?

Hope this helps,
PJ

@pieterjanvc thank you for your help.
Regarding your question "Why do you need to store user specific files anyway? " I gonna extend my previous explaination. A user upload a database and select several parameters that returns several files with e specific names e. g. Results_of_test_A, Results_of_test_B, etc., the names are always the same so the user knows what results does the file store. I need to create a user specific file so the files created by two different user won't conflict, making a user getting the results of other.
You mention "You should remember to erase the folder after the task finished... " How I do that?

This topic was automatically closed 7 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.