在Rstudio中处理项目根 [英] address project root in Rstudio

查看:350
本文介绍了在Rstudio中处理项目根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在RStudio中打开或创建项目时,工作目录将自动更改为项目的工作目录。但是,如果要保存文件或在其他子目录中进行工作,那么更改工作目录通常很方便。在每个脚本中可能会很痛苦地管理这个。

When you open or create a project in RStudio, the working directory is automatically changed to that of the project. However, if you are saving files or doing work in other subdirectories, it's often convenient to change the working directory then. It can get to be a pain to manage this in every script.

是否有一个变量总是指向项目根(也可以由会话分支编织HTML按钮)可以用来使这更容易?

Is there a variable that always points to the project root (that is also readable by the session forked by the "Knit HTML" button) that can be used to make this easier?

编织HTML按钮似乎将工作目录设置为R Markdown文件的工作目录。例如,如果您的 ./ Rprofile 中有一个名为 project.root 的变量,并单击编织HTML 打开此脚本,

The "Knit HTML" button appears to set the working directory to that of the R Markdown file. For example, if you have a variable called project.root in your ./Rprofile, and you click "Knit HTML" with this script opened,

```{r}
getwd()
source('./Rprofile')
setwd(project.root)
getwd()
```

第一个和最后一个结果将是脚本目录,其余的将抛出错误。

the first and last result will be the script directory and the rest will throw errors.

推荐答案

如您所推断,您可以在命令行中设置R的工作目录,如下所示:

To recap, as you inferred, you can set R's working directory at the command line like so:

setwd(〜/ NateProjects)

setwd("~/NateProjects")

您还可以使用RStudio的文件窗格导航到一个目录,然后从菜单中将其设置为工作目录:会话 - >设置工作目录 - >到文件窗格位置。 (你会看到更多的选择)。或者在文件窗格中,选择更多和设置为工作目录。

You can also use RStudio's Files pane to navigate to a directory and then set it as working directory from the menu: Session --> Set Working Directory --> To Files Pane Location. (You'll see even more options there). Or within the Files pane, choose More and Set As Working Directory.

但是,您可以做得更好建立一个像专业程序员的环境。为此,您可以选择将所有与项目关联的文件(一个项目被松散地定义为将我的所有个人代码都放在一个项目下)组织在一起 - 输入数据,R脚本,分析结果,数字。事实上,RStudio已经内置了对此的支持。这里有一个很好的教程,你可以看一下如何逐步详细说明一下:

But, you can do better & set up an environment like a professional programmer. To do this, you can choose to keep all the files associated with a project (a project being loosely defined as I have all my personal code under one project) organized together -- input data, R scripts, analytical results, figures. In fact, RStudio has built-in support for this. There is an excellent tutorial here that you can have a look at which explains how to walk through in step by step detail:

http://www.rstudio.com/ide/docs/using/projects

从本文中,您需要执行以下操作:

Essentially, taking from the documents, you need to do the following:


  • 使用Create Project命令创建一个新项目(在项目菜单和全局工具栏上可用)。这可以是将所有未来的子项目和子文件夹放在

中的地方,然后,一旦在RStudio中创建了这个新项目,则会发生以下操作:

Then, once you create this new project in RStudio, the following actions happen:


  • 您可以在项目目录中创建一个项目文件(带有.Rproj扩展名)。此文件包含各种项目选项(如下所述),也可用作直接从文件系统打开项目的快捷方式。

  • 您创建一个隐藏目录(名为.Rproj.user)其中存储项目特定的临时文件(例如自动保存的源文档,窗口状态等)。此目录也会自动添加到.Rbuildignore,.gitignore等(如果需要)。

  • 将项目加载到RStudio中,并在项目工具栏(位于远处)显示其名称主工具栏右侧)

这篇关于在Rstudio中处理项目根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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