让 R 存储每个会话的工作目录

发布于 2024-08-09 23:29:14 字数 139 浏览 6 评论 0原文

我在超级用户上问过这个问题,但有人建议我在这里提问,因为有更多的 R 专家。

The question:

当我转到“文件”>“文件”时,我必须继续导航到我的目录。更改目录...,这特别烦人。

有谁知道如何让R记住以前使用过的目录?

I asked this on Super User, but someone suggested that I take it here because there are many more R experts.

The question:

I have to keep navigating to my directory when I go to File > Change dir..., which is particularly annoying.

Does anyone know how to make R remember the previously used directory?

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

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

发布评论

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

评论(5

っ〆星空下的拥抱 2024-08-16 23:29:14

我将与特定项目相关的所有代码保存在一个文件中(或更常见的是一系列文件)。第一行通常是

setwd(...)

设置目录。

将工作区保存到所需目录后,只需打开该工作区(而不是从桌面或开始菜单)即可启动 R。那么目录已经设置到你想要的位置了。

I keep all the code associated with a particular project in a file (or more often a series of files). The first line is usually

setwd(...)

which sets the directory.

Once a workspace has been saved in the desired directory, just start R by opening that workspace (rather than from the desktop or start menu). Then the directory is already set to where you want it.

泪冰清 2024-08-16 23:29:14

我可能不会回答你的问题,因为它有点模糊,但有一些想法:

  1. 你可以将“我的目录”的位置存储在 R 的 .GlobalEnv 中,以便当你启动 R 时它从那里开始。
  2. 本文讨论如何拥有不同的工作目录以及相应的不同“.RData”文件。
  3. 您可以编写一个自定义函数,在设置新目录之前记住当前目录

    cd <- function(x = "") {   
        逻辑(长度 = 0)
        if (!is.logic(x)) {    
            cwd <- getwd()   
            Sys.setenv("R_OLDWD"=cwd)
            设置wd(x)    
          } 别的 {
            setwd(打印(粘贴(Sys.getenv("R_OLDWD"))))
        }
    }
    

来自 R for Windows 常见问题解答

工作目录是启动 Rgui 或 Rterm 的目录,除非在快捷方式属性的“开始于”字段指定时使用了快捷方式。您可以通过调用 getwd() 从 R 代码中找到它。

主目录设置如下:如果设置了环境变量 R_USER,则使用其值。否则,如果设置了环境变量 HOME,则使用其值。在这两个用户可控的设置之后,R 尝试查找系统定义的主目录。它首先尝试使用 Windows“个人”目录(在 Windows XP 上通常为 C:\Documents and Settings\username\My Documents,在 Vista 上通常为 C:\Users\username\Documents)。如果失败,并且环境变量 HOMEDRIVE 和 HOMEPATH 均已设置(通常是这样),则该值为 ${HOMEDRIVE}${HOMEPATH}。如果所有这些都失败,则使用当前工作目录。

您可以通过 Sys.getenv("R_USER") 从 R 代码中找到它。

I may not be answering your question, because it's a bit vague, but some thoughts:

  1. You can store the location of 'my directory' in R's .GlobalEnv so that it starts there when you start R.
  2. This article discuses how to have different working directories with corresponding different ".RData" files.
  3. You could write a custom function that remembers the current directory before you set the new directory

    cd <- function(x = "") {   
        logical (length = 0)
        if (!is.logical(x)) {    
            cwd <- getwd()   
            Sys.setenv("R_OLDWD"=cwd)
            setwd(x)    
          } else {
            setwd(print(paste(Sys.getenv("R_OLDWD"))))
        }
    }
    

From the R for Windows FAQ:

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. You can find this from R code by the call getwd().

The home directory is set as follows: If environment variable R_USER is set, its value is used. Otherwise if environment variable HOME is set, its value is used. After those two user-controllable settings, R tries to find system-defined home directories. It first tries to use the Windows "personal" directory (typically C:\Documents and Settings\username\My Documents on Windows XP and C:\Users\username\Documents on Vista). If that fails, if both environment variables HOMEDRIVE and HOMEPATH are set (and they normally are), the value is ${HOMEDRIVE}${HOMEPATH}. If all of these fail, the current working directory is used.

You can find this from R code by Sys.getenv("R_USER").

隐诗 2024-08-16 23:29:14

这取决于您使用的系统。有一些技巧可以使用,但如果您希望从系统菜单运行 R 并让它记住目录,那么快速回答是否定的,这不会发生。 Linux 非常简单,只需先导航到终端中的目录即可解决问题。我对Macs一无所知,但我可以广泛地谈论Windows。首先,如果您导航到该目录并保存工作区一次,那么您可以使用保存的 .RData 文件双击并恢复您的工作区(包括当前目录)。我个人的、有偏见的观点是使用像 Notepad++NppToR 这样,当您生成 Rgui 窗口时,您将从当前脚本继承活动目录。它还提供了一个菜单命令来将工作目录调整为当前脚本的目录。

另一点是,您始终可以在任何平台上的任何 R 会话中使用 setwd("path/to/dir/") 命令设置工作目录。

This depends on the system that your using. There are a few tricks to use but if your looking to run R from a system menu and have it remember the directory the quick answer is no that won't happen. Linux is pretty easy just navigate to the directory in the terminal first and that will solve the problem. I have no idea about macs, But I can talk about windows extensively. First if you navigate to the directory and save your workspace once then you can use the saved .RData file to double click and restore your workspace including current directory. My personal, and biased opinion is to use an editor like Notepad++ with NppToR that way when you spawn a Rgui window you inherit the active directory from the current script. It also provides a menu command to adjust the working directory to the current script's directory.

Another point is that you can always set the working directory with the setwd("path/to/dir/") command inside any R session on any platform.

一向肩并 2024-08-16 23:29:14

我使用 StatET 和 Eclipse 作为我的 R 用户界面。它会自动将工作目录设置为我的项目文件夹的位置。工作空间 = ${project_loc}。
当从特定项目启动 R 时,它还会自动加载任何已保存的工作区。

I use StatET and Eclipse as my R user interface. It automatically sets the working directory to the location of my project folder. workspace = ${project_loc}.
It also automatically loads any saved workspace, when starting R from a particular project.

清晰传感 2024-08-16 23:29:14

在 Windows 上,我将 code.google.com/p/batchfiles 中的文件 Rgui.bat 放入我的项目目录中,并使用它来启动 R。

On Windows, I put a file Rgui.bat from code.google.com/p/batchfiles in my project directory and use this to start R.

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