2

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
wibeasley
  • 5,000
  • 3
  • 34
  • 62
Tom
  • 29
  • 5

3 Answers3

5

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.

Scarabee
  • 5,437
  • 5
  • 29
  • 55
G5W
  • 36,531
  • 10
  • 47
  • 80
  • this is very useful. I think and it would also lead to a soultion, but when I run `setwd(Paths[Sys.info()[7]])` it says `missing value is invalid`. So there's something missing and I do not find what it is. Could you explain what the "7" means in `Sys.info()[7]`? Thank you! – Tom Jul 01 '18 at 13:19
  • okay now it works and I just had a mistake by calling the User wrong! Thanks so much - this is really awesome and it works with every User! – Tom Jul 01 '18 at 17:06
  • Sorry, I should have warned you to check the way that `Sys.info` returns the username. All Caps on my machine. I am glad that it helped. – G5W Jul 01 '18 at 17:43
0

Dropbox provides a json file that can be used to set the directory

library(magrittr)
library(jsonlite)

DropboxInfo <- 
  if (Sys.getenv("OS") == "Windows_NT") {
    file.path(Sys.getenv("LOCALAPPDATA"), "Dropbox", "info.json")
  } else {
    "~/.dropbox/info.json"
  }

Path2Dropbox <- 
  jsonlite::fromJSON(DropboxInfo) %>%
  use_series("business") %>%  # or 'personal' if applicable
  use_series("path")

Datensatz <- read_excel(file.path(Path2Dropbox, "Datensatz.xlsx"))
Hugh
  • 15,521
  • 12
  • 57
  • 100
  • when I use this code it says 'Error: `path` does not exist: ‘C:\Users\thoma\Dropbox/Datensatz.xlsx’' is there a chance to change the subfolders? 'C:/Users/thoma/Dropbox/FOM/Empirisches Projekt/Gruppenarbeit/' – Tom Jul 01 '18 at 13:05
  • In your OP you use that path as an example. Is that not correct? – Hugh Jul 01 '18 at 13:07
  • 'C:/Users/thoma/Dropbox/FOM/Empirisches Projekt/Gruppenarbeit/' is the path I need to find the _Datensatz.xlsx_. So can I set this path in any way? – Tom Jul 01 '18 at 13:14
  • Yes `read_excel(file.path(Path2Dropbox, "FOM/Empirisches Projekt/Datensatz.xlsx"))` – Hugh Jul 01 '18 at 13:24
  • thanks again. this is a also a great way to set the directory automatically but not so usefull with multiple users, becuase in the end you have to set a path that is used specifically. – Tom Jul 01 '18 at 17:10
0

I found an easy way to do that among multiple users of Dropbox.

Suppose you stored your shared data in Dropbox with the following path: "C:/Users/thoma/Dropbox/Data/Datensatz.xlsx", and your R script is in "C:/Users/thoma/Dropbox/Code/data.R".

Open your code from this folder, and RStudio will automatically set Code as the current working directory. Then you can change the working directory to Data in your script.

setwd("../../../../Data/") ## set folder 'Data' as the working directory
Datensatz <- read_excel("./Datensatz.xlsx") ## use a relative path from the working directory
jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
bruce1630
  • 1
  • 3