Reputation: 19
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
Upvotes: 1
Views: 2650
Reputation: 1
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
Upvotes: 0
Reputation: 37641
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.
Upvotes: 4
Reputation: 16090
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"))
Upvotes: 0