Skip to content
Prev 180000 / 398513 Next

Set working directory by dragging text file onto R shortcut? [WinXP, unfortunately]

On 11/05/2009 6:30 PM, Mark Na wrote:
You can execute a script on startup as follows:

1.  Put the script into a file somewhere, e.g. c:/temp/test.R

2.  Edit your shortcut to R to set the command to something like this:

path\to\Rgui.exe  R_PROFILE_USER=c:/temp/test.R --args

Now your script will execute on startup, and you can have it change 
directory, load a file, whatever you want.  For example, this changes 
directory to the directory of the file dropped on the shortcut, then 
opens the file for editing:

Contents of test.R:

  filename <- commandArgs(TRUE)
  if (length(filename)) {
    setwd(dirname(filename))
    utils::file.edit(filename)
  }

You can make it as involved as you like, e.g. looking at the file 
extension to determine what to do with it, etc.

Duncan Murdoch