Support

Sharing a Project with multiple users

Follow

If you are using RStudio Projects, you may want to share a project with multiple users on the same network in order to work collaboratively on the same project. However, the default R setup uses the same .Rhistory and .RData files for all users accessing the project which will lead to conflicts.

You can solve this by creating a project in a location accessible to all the users you wish to be able to edit it and adding in the following code in a .Rprofile file in that project’s working directory. This will cause each user's .Rhistory and .RData to be saved to separate files within a subdirectory of the project folder.

# define user data directory and history file location
local({
  dataDir <- "userdata"
  if (identical(.Platform$OS.type, "windows"))
    username <- Sys.getenv("USERNAME")
  else
    username <-  Sys.getenv("USER")                   
  userDir <- file.path(dataDir, username)
  if (!file.exists(userDir))
    dir.create(userDir, recursive = TRUE)
  userDir <- normalizePath(userDir)
  
  # locate history in user data dir
  Sys.setenv(R_HISTFILE = file.path(userDir, ".Rhistory"))
  
  # make the rdataPath available in the global environment
  # so it can be accessed by .First and .Last
  rdataPath <- file.path(userDir, ".RData")
  assign(".rdataPath", rdataPath, envir = globalenv())
})

# load the .RData at startup
.First <- function() {
  if (file.exists(.rdataPath))
    load(.rdataPath, envir = globalenv())
}

# save the .RData at exit
.Last <- function() {
  save.image(.rdataPath)
}

You will also need to set the following options for your project in the Project Options menu:

If you want to share a project with multiple users without using .RData, you can do so using the following code in the project's .Rprofile:

# define user data directory and history file location
local({
  dataDir <- "userdata"
  if (identical(.Platform$OS.type, "windows"))
    username <- Sys.getenv("USERNAME")
  else
    username <-  Sys.getenv("USER")                   
  userDir <- file.path(dataDir, username)
  if (!file.exists(userDir))
    dir.create(userDir, recursive = TRUE)
  userDir <- normalizePath(userDir)
  
  # locate history in user data dir
  Sys.setenv(R_HISTFILE = file.path(userDir, ".Rhistory"))
})
 

Comments