开发者

Getting R to store the working directory for every session

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 mak开发者_开发问答e R remember the previously used directory?


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.


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").


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜