views:

139

answers:

5

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?

+1  A: 

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

DaveParillo
+2  A: 

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.

Rob Hyndman
A: 

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.

Jeromy Anglim
A: 

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

Karsten W.
+1  A: 

Maybe this question will help.

Marek