上升/向下几个级别时,如何正确指定相对路径

发布于 2025-02-05 20:48:11 字数 784 浏览 4 评论 0原文

我有以下文件夹结构:

04  
 |--folder 1  
07  
 |--folder 2  
   |--folder 3  
     |-- ABC.rproj 

因此,我在07文件夹中有一个R项目两个级别。

我现在想从04中的文件夹1加载文件,但是我总是会遇到错误,我不明白为什么。我在许多许多项目中都使用相对路径,而且效果很好,只是在这里,所以我的猜测是因为rproj和工作目录的事实是两个级别下降了吗?

假设文件夹1中有一个excel文件,称为“ test.xlsx”,然后我通常会读取以下内容:

library(readxl)
df <- read_excel("../../04/folder 1/test.xlsx")

但是它不起作用。

我要去哪里?

很奇怪的是,检查文件存在的工作起作用,即list.files(“ ../../ 04/folder 1/“,staters =” .xslx“)返回[1 ] test.xlsx

编辑:它有效直到某个路径深度为止,即如果将文件放置一个级别,则它将读取它。

不起作用:

df <- read_excel("../../04/A/B/C/D/E/F/test.xlsx")

确实有效:

df <- read_excel("../../04/A/B/C/D/E/test.xlsx")

I have the following folder structure:

04  
 |--folder 1  
07  
 |--folder 2  
   |--folder 3  
     |-- ABC.rproj 

So I have an R project two levels down in the 07 folder.

I now want to load a file from folder 1 in 04, but I always get an error and I don't understand why. I'm using relative paths in many, many projects and it works just fine, just not here, so my guess is it is because of teh fact that the rproj and hence the working directory is two levels down?

Assume there's an Excel file in folder 1 called "test.xlsx" then I would usually read that in with:

library(readxl)
df <- read_excel("../../04/folder 1/test.xlsx")

But it doesn't work.

Where am I going wrong?

The weird thing is, checking the file existence works, i.e. list.files("../../04/folder 1/", pattern = ".xslx") returns [1] test.xlsx

Edit: it works until a certain path depth, i.e. if I put the file one level up it reads it in.

Doesn't work:

df <- read_excel("../../04/A/B/C/D/E/F/test.xlsx")

Does work:

df <- read_excel("../../04/A/B/C/D/E/test.xlsx")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文