简体   繁体   English

如何让 R 将您的工作目录识别为其工作目录?

[英]How to get R to recognize your working directory as its working directory?

I use R under Windows on several machines.我在多台机器上的 Windows 下使用 R。

I know you can set the working directory from within an R script, like this我知道你可以从 R 脚本中设置工作目录,就像这样

setwd("C:/Documents and Settings/username/My Documents/x/y/z")

... but then this breaks the portability of the script. ...但是这破坏了脚本的可移植性。 It's also annoying to have to reverse all the slashes (since Windows gives you backslashes)必须反转所有斜杠也很烦人(因为 Windows 给了你反斜杠)

Is there a way to start R in a particular working directory so that you don't need to do this at the script level?有没有办法在特定的工作目录中启动 R,这样您就不需要在脚本级别执行此操作?

You should copy shortcut to R (R.lnk file) to desire folder.您应该将 R(R.lnk 文件)的快捷方式复制到所需文件夹。 Then in "Properties" (right mouse button -> last option) delete anything in field "Start in..." in second tab ("Shortcut"?).然后在“属性”(鼠标右键 - > 最后一个选项)中删除第二个选项卡(“快捷方式”?)中“开始于...”字段中的任何内容。 If you start R with this shortcut working directory will be that one where the shortcut is.如果您使用此快捷方式启动 R,工作目录将是快捷方式所在的目录。

I don't have english version of Windows so I'm not sure about field names, but they should be easy to find.我没有英文版的 Windows,所以我不确定字段名称,但它们应该很容易找到。

Similar questions were in R-windows-faq: R-windows-faq 中有类似的问题:

2.5 How do I run it? 2.5 我如何运行它?

2.10 How can I keep workspaces for different projects in different directories? 2.10 如何将不同项目的工作空间保存在不同的目录中?

2.14 What are HOME and working directories? 2.14 什么是HOME和工作目录?

In 2.14 is mentioned that在 2.14 中提到

The working directory is the directory from which Rgui or Rterm was launched, unless a shortcut was used when it is given by the `Start in' field of the shortcut's properties.工作目录是启动 Rgui 或 Rterm 的目录,除非在快捷方式属性的“开始于”字段中使用了快捷方式。

You could use an environmental variable.您可以使用环境变量。 This can work with Sys.getenv() and Sys.setenv() .这可以与Sys.getenv()Sys.setenv() For instance:例如:

> Sys.setenv(R_TEST="testit")
> Sys.getenv("R_TEST")
  R_TEST 
"testit" 

If you sent the variable in your script, you should be able to access it from within, and then call setwd() on that output.如果您在脚本中发送了变量,您应该能够从内部访问它,然后在该输出上调用setwd()

将您的工作区保存到所需目录,然后您只需从 Windows 资源管理器中打开工作区。

I put the following line in front of my scripts and it allows me to work across my computers.我将以下行放在我的脚本前面,它允许我在我的计算机上工作。

setwd(path.expand("~/path/to/working/directory/") )

where ~ is = to your home directory.其中 ~ 是 = 到您的主目录。

Sys.setenv(HOME = "path") or Sys.setenv(R_USER = "path") can both set the home directory. Sys.setenv(HOME = "path")Sys.setenv(R_USER = "path")都可以设置主目录。

In my case, I work on several windows boxes, each have fairly different directory structures, but by setting the home directory properly I can sync code between computers and have them run properly on each one since where I run my R projects have similar directory structures.就我而言,我在几个 Windows 机器上工作,每个机器都有相当不同的目录结构,但是通过正确设置主目录,我可以在计算机之间同步代码并使它们在每个机器上正常运行,因为我运行 R 项目的地方具有相似的目录结构.

If you're using Emacs/ESS, this isn't a problem.如果您使用 Emacs/ESS,这不是问题。 I navigate to the directory where my R script is located, open it, then start an R ESS process.我导航到 R 脚本所在的目录,打开它,然后启动 R ESS 进程。 An R console pops up with the current directory as R's working directory.一个 R 控制台弹出,当前目录作为 R 的工作目录。

If you haven't converted to Emacs/ESS, I recommend it.如果你还没有转换成 Emacs/ESS,我推荐它。 (Though to prevent a flame war, I also note there are similar options for Vi users.) (虽然为了防止爆发战争,我还注意到 Vi 用户也有类似的选择。)

Hope that helps.希望有帮助。

Just a detail: instead of reversing the slashes as you say, just add another backslash.只是一个细节:不要像你说的那样反转斜杠,只需添加另一个反斜杠。 Two of these \\\\ works the same way as one of these /.其中两个 \\\\ 的工作方式与其中一个 / 相同。 That makes it at least a little easier.这至少使它变得容易一些。

For Ubuntu:对于 Ubuntu:
Insert the following command into your .Rprofile file (usually in your home directory):将以下命令插入您的.Rprofile文件(通常在您的主目录中):

setwd(Sys.getenv("PWD"))

Now your default working directory will be whatever directory you launched R from.现在您的默认工作目录将是您启动 R 的任何目录。 Keep in mind you can also set up default workspaces in different directories by saving your workspace image as .RData wherever you plan to launch R (startup sources .Rprofile before searching for .Rdata in the cwd ).请记住,您也可以通过保存您的工作空间形象建立在不同的目录默认工作区.RData无论你(启动源计划推出[R .Rprofile搜索之前.Rdatacwd )。

To set the R work directory like the current directory of the R script that I'm working, I always use a combination of the commands getwd() and setwd() , like this:要将 R 工作目录设置为我正在工作的 R 脚本的当前目录,我总是使用命令getwd()setwd() ,如下所示:

path <- getwd() setwd(path)

or要么

setwd(getwd())

If you want learn more about it, see this article .如果您想了解更多信息,请参阅这篇文章

Cheers,干杯,

[]'s [] 的

To set working directory in R Studio: Refer detailed slide deck with screen-shots here .要在 R Studio 中设置工作目录:在此处参考带有屏幕截图的详细幻灯片。

  1. Using setwd(): windows users would need to replace backward slashes '' with forward slashes '/' or double backward slashes '\\' You can do the former using find & replace (Short-cut: Ctrl+F)使用 setwd():Windows 用户需要用正斜杠 '/' 或双反斜杠 '\\' 替换反斜杠 '' 您可以使用查找和替换(快捷键:Ctrl + F)
  2. Another option: Go to Session --> set working directory --> choose working directory & browse the folder which you want to set as the working directory, click on open另一个选项:转到会话 --> 设置工作目录 --> 选择工作目录并浏览要设置为工作目录的文件夹,单击打开
  3. Quickest method (my favorite) use the shortcut 'Ctr+Shift+H' (on windows system), browse the folder which you want to set as the working directory, click on open最快的方法(我最喜欢的)使用快捷键'Ctr+Shift+H'(在windows系统上),浏览要设置为工作目录的文件夹,点击打开

To set a permanent working directory (when not in a project) in R Studio: Refer my quick video on the same: https://youtu.be/hMjzO4bAi70要在 R Studio 中设置永久工作目录(不在项目中时):请参阅我的快速视频: https : //youtu.be/hMjzO4bAi70

Go to Tools --> Global Options --> R General [Basic] --> Default Working Directory (when not in a project) browse the folder which you want to set as the working directory, click on 'Apply' and 'OK'转到工具 --> 全局选项 --> R 常规 [基本] --> 默认工作目录(不在项目中时)浏览要设置为工作目录的文件夹,单击“应用”和“确定” '

在此处输入图片说明

However, the efficient & better way to organize your work is to create projects & use version control.但是,组织工作的有效且更好的方法是创建项目并使用版本控制。

Put a shortcut for the R gui into your desired directory.将 R gui 的快捷方式放入所需目录。 Right-click and look at the shortcut properties.右键单击并查看快捷方式属性。 Delete the entry for "Start In" and click OK.删除“Start In”条目,然后单击“确定”。 When you launch the R gui from this shortcut the default directory will be the folder from which you have launched.当您从此快捷方式启动 R gui 时,默认目录将是您启动的文件夹。 Copy/paste this shortcut wherever you desire.将此快捷方式复制/粘贴到您想要的任何位置。

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

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