工作目录设置如何影响 RStudio 中创建的 R 项目?

发布于 2025-01-15 15:15:08 字数 49 浏览 2 评论 0原文

工作目录设置如何影响 RStudio 中创建的 R 项目?我明白了,但无法准确解释。

How does the working directory setting affect the created R project in RStudio? I get it but can't explain it exactly.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短暂陪伴 2025-01-22 15:15:08

脚本引用的所有文件路径都相对于会话的当前工作目录。假设我们需要加载位于 ~/my_project/file.csv 的文件。如果没有明确的工作目录,您可能需要指定完整的文件路径:

read_csv('~/my_project/file.csv')

但是设置了工作目录后,您可以这样做:

setwd('~/my_project/')
read_csv('file.csv')

RStudio 也知道当前的工作目录。在一对空引号内按 Tab 键,自动完成功能将显示当前工作目录中的文件。创建或加载 RStudio 项目文件还会将工作目录设置为项目文件的当前位置。还值得记住的是,工作目录是按会话设置的,而不是按脚本设置的(当用户同时打开多个脚本并错误地期望每个脚本“记住”不同的工作目录时,这有时会导致混乱)。

All file paths referenced by a script are relative to the session's current working directory. Suppose we need to load a file located at ~/my_project/file.csv. Without an explicit working directory, you might need to specify the full file path:

read_csv('~/my_project/file.csv')

But with the working directory set, you could just do:

setwd('~/my_project/')
read_csv('file.csv')

RStudio is also aware of the current working directory. Press Tab inside a pair of empty quotes, and the autocomplete will display files in the current working directory. Creating or loading an RStudio project file also sets the working directory to the current location of the project file. It's also worth keeping in mind that that working directory is set per session, not per script (this sometimes causes confusion when users have multiple scripts open at once and mistakenly expect each script to "remember" a different working directory).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文