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

人盡茶涼 提交于 2019-12-24 14:06:11

问题


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 everyones 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 everytime a different user is running the code.

Thanks for your help!

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


回答1:


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.




回答2:


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"))


来源:https://stackoverflow.com/questions/51122891/ask-to-set-the-working-directory-in-r-studio-multiple-users-working-with-the-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!