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?
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?
I may not be answering your question, because it's a bit vague, but some thoughts:
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").
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 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.