rworking-directorymultiple-users

Ask to set the working directory in R Studio - multiple users working with the same R script


We are three people using the same R script to work on our research project in R Studio. This brings some issues by setting the working directory, because the file and the data sheets are saved locally in everyone's Dropbox folder. So we use the same script and the same data but the path to the working directory is for example like 'C:/Users/thoma/Dropbox/...' in my case.

I can set the wd by setwd("directory") at the beginning of our code, but this works for me only.

My Question: Is there a command that asks me where to set my wd that every user can set his own working directory like askforwd()

The data in each folder is synced so this is the only path that has to be changed every time a different user is running the code.

Here's an example of our code:

setwd("C:/Users/thoma/Dropbox/") #sets the directory
Datensatz <- read_excel("Datensatz.xlsx") #reads the synced data in the folder

Solution

  • Instead of making the user set the directory, just build all of them into the script and check which user is using the script.

    Paths = c("C://user/Fred/", "C://user/Wilma", "C://Some/other/path")
    names(Paths) = c("Fred", "Wilma", "Guest")
    setwd(Paths[Sys.info()[7]])
    

    Of course, Sys.info()[7] gives the user that is currently logged in.