简体   繁体   English

“../”相对路径目录不起作用 R

[英]“../” relative path directory not working R

I often have to work with shared data/code from Dropbox, and all of the files are usually loaded in as df <- import("../data/branch/file_name.csv")我经常需要处理来自 Dropbox 的共享数据/代码,所有文件通常都以df <- import("../data/branch/file_name.csv")的形式加载

However, this never loads on my computer, so I always have to type in my full directory as df <- import("C:/Users/Name/Dropbox/Folder1/Folder2/data/branch/file_name.csv")但是,这永远不会加载到我的计算机上,所以我总是必须在我的完整目录中df <- import("C:/Users/Name/Dropbox/Folder1/Folder2/data/branch/file_name.csv")

Which makes it difficult for others to reproduce the code.这使得其他人难以重现代码。 Is there any way to fix this so that R can read the "../" part properly?有没有办法解决这个问题,以便 R 可以正确读取“../”部分? I always get this error: Error: path does not exist: ../data/branch/file_name.csv我总是收到此错误:错误: path不存在:../data/branch/ ../data/branch/file_name.csv

(I used the import function as an example but this problem occurs with other packages or loading functions as well) (我以 import function 为例,但其他包或加载函数也会出现此问题)

I've also tried using "~/" but the problem persists.我也尝试过使用“~/”,但问题仍然存在。 The shared code I work with always uses "../" though so I'd prefer a solution that gets that to work instead so that I don't have to change the original script each time I need to use it.我使用的共享代码总是使用“../”,所以我更喜欢让它工作的解决方案,这样我就不必在每次需要使用它时更改原始脚本。

Thank you very much.非常感谢。

Edit: Despite the conversation below, I am still having trouble with this.编辑:尽管在下面进行了对话,但我仍然遇到了麻烦。 I tried setting the working directory and then using "../" and it sometimes works for read_xls() (it was working a few hours ago, not anymore, depsite doing the exact same thing and setting the same working directory) but not for rio::import() or other reading functions.我尝试设置工作目录,然后使用“../”,它有时适用于read_xls() (它在几个小时前工作,不再是,depsite 做完全相同的事情并设置相同的工作目录)但不适用于rio::import()或其他读取函数。 I've even tried setting my working directory to the exact same folder and using "./" but no luck.我什至尝试将我的工作目录设置为完全相同的文件夹并使用“./”但没有运气。 Errors now say "Cannot open the connection" or "No such file"错误现在说“无法打开连接”或“没有这样的文件”

You can try setting your working directory(wd) to the current folder first, then use read.csv to import the data.您可以先尝试将工作目录(wd)设置为当前文件夹,然后使用 read.csv 导入数据。

  1. To set the working directory, first obtain the current directory using rstudioapi::getActiveDocumentContext()$path then use setwd() to set the wd to this path.要设置工作目录,首先使用rstudioapi::getActiveDocumentContext()$path获取当前目录,然后使用setwd()将 wd 设置为该路径。
  2. Then use whatever function to import your data.然后使用任何 function 导入您的数据。

For example, let's say your R code is saved in the "branch" folder, then the following code should work just fine:例如,假设您的 R 代码保存在“分支”文件夹中,那么以下代码应该可以正常工作:

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
mydata <- read.csv("./file_name.csv")
head(mydata)

Otherwise, if your R code was saved in a subfolder under Folder2 called code such that your R script's directory is: "C:/Users/Name/Dropbox/Folder1/Folder2/code/mycode.R" , then the following should work:否则,如果您的 R 代码保存在名为codeFolder2下的子文件夹中,则您的 R 脚本的目录为: "C:/Users/Name/Dropbox/Folder1/Folder2/code/mycode.R" ,那么以下应该有效:

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
mydata <- read.csv("../data/branch/file_name.csv")
head(mydata)

It looks like the person you are collaborating with has their working directory set to a separate folder within Folder2 (perhaps a folder with all the.R code?).看起来您正在与之合作的人将他们的工作目录设置为 Folder2 中的一个单独文件夹(可能是一个包含所有.R 代码的文件夹?)。 The .. means that the relative path is referencing the next folder up from the working directory (the next folder up is presumably Folder2). ..表示相对路径正在引用工作目录中的下一个文件夹(下一个文件夹可能是 Folder2)。

To solve your problem, every time you start working you need to set your working directory to the same folder that your collaborator is using for their working directory.为了解决您的问题,每次您开始工作时,您都需要将您的工作目录设置为您的协作者用于他们的工作目录的同一文件夹。 For example, this may be something like setwd("C:/Users/Name/Dropbox/Folder1/Folder2/RCode") .例如,这可能类似于setwd("C:/Users/Name/Dropbox/Folder1/Folder2/RCode") Unfortunately you will have to do that every time you open up the script, but if you do it at the very beginning, then the rest of the code should work.不幸的是,每次打开脚本时都必须这样做,但如果你在一开始就这样做,那么代码的 rest 应该可以工作。

Working directories can be hard to manage, especially when collaborating.工作目录可能很难管理,尤其是在协作时。 A better solution to the working directory problem is to create a project in R Studio and collaborate on that project using GitHub.工作目录问题的更好解决方案是在 R Studio 中创建一个项目,并使用 GitHub 协作处理该项目。 If you don't have experience with GitHub, these videos are an approachable resource for helping you get started.如果您没有使用 GitHub 的经验, 这些视频是帮助您入门的平易近人的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM