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

Duncan Murdoch murdoch at stats.uwo.ca
Tue May 12 02:16:31 CEST 2009


On 11/05/2009 6:30 PM, Mark Na wrote:
> Hi R-helpers,
> 
> I must use WinXP at work, and I'm missing a particular feature that's
> available in R on my Mac at home...
> 
> In WinXP I would like to launch R by dragging and releasing a text
> file on top of the R shortcut on my desktop. Then, I would like the
> working directory to be automatically set to the location of that text
> file.
> 
> That's what works in MacOS, but I can't figure out if it's possible in WinXP.
> 
> Any ideas? I'd appreciate it...
> 
> Thanks, Mark Na
> 
> PS What I do now is make a new R shortcut for each project, store that
> shortcut in the directory containing that project's datafiles and R
> programs (which are in text files), and set the "Start in" value to
> that directory...but this has to be set up uniquely for each
> project...


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




More information about the R-help mailing list