要求在 R Studio 中设置工作目录 - 多个用户使用相同的 R 脚本 [英] Ask to set the working directory in R Studio - multiple users working with the same R script

查看:73
本文介绍了要求在 R Studio 中设置工作目录 - 多个用户使用相同的 R 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们三个人使用相同的 R 脚本在 R Studio 中开展我们的研究项目.这通过设置工作目录带来了一些问题,因为文件和数据表本地保存在每个人的Dropbox文件夹中.所以我们使用相同的脚本和相同的数据,但在我的例子中,工作目录的路径类似于C:/Users/thoma/Dropbox/...".

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.

我可以在代码的开头通过 setwd("directory") 设置 wd,但这仅适用于我.

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

我的问题:是否有命令询问我在哪里设置我的 wd,每个用户都可以设置自己的工作目录,例如 askforwd()

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.

感谢您的帮助!

这是我们的代码示例:

setwd("C:/Users/thoma/Dropbox/") #设置目录

Datensatz <- read_excel("Datensatz.xlsx") #读取文件夹中的同步数据

推荐答案

不要让用户设置目录,只需将它们全部构建到脚本中并检查哪个用户正在使用该脚本.

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

当然,Sys.info()[7] 给出当前登录的用户.

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

这篇关于要求在 R Studio 中设置工作目录 - 多个用户使用相同的 R 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆