1

I need to share my data and R source files with a coworker who doesn't have any experience with command line. Moreover, I work in Linux while she's under Windows. But she would like to change some constants and recalculate the scripts. So, it would be cool if she could just double click the R source file and R will be executing in the same directory where the source and data files lie. I thought about setting

Rscript -e "source(\"%1\",chdir=TRUE)" 

to the association key in the registry, but the filename (%1) will contain backslashes which R will not handle.

Another way is to setwd() to the source directory in the beginning of my script, but I don't know how to obtain it. AFAIK, argv[0] will be R.exe, not the source.R.

Using GUI is not very convenient, either, because it requires to separately change directory and then to load the script.

Do I have to write a R loader (exe or cmd) for this?

Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
Arseny
  • 207
  • 2
  • 6
  • You might try the methode I use; see bottom of http://stackoverflow.com/questions/10098189/disable-saving-history/10103274#10103274 – Dieter Menne Apr 25 '12 at 06:55
  • I'm confused: typically R converts Windows backslashes (internally) and properly follows directory paths. You sure it fails? Next, let me ask: Can you do what you want by setting PATH environment values in her `.Rprofile` file and working with `file.path` ? – Carl Witthoft Apr 25 '12 at 11:07
  • Here's a link to a [possibly helpful answer](http://stackoverflow.com/a/9132685/210673) – Aaron left Stack Overflow Apr 25 '12 at 13:32
  • Strange - neither of approaches is working. – Arseny Apr 26 '12 at 00:32
  • Strange - neither of approaches from the links are working. "pushd %L" in the shell/open/command doesn't work because %L expands to full filename (not the directory). attr('srcref') returns NULL at my Linux system and in Windows. dirname(sys.frame(1)$ofile) is version-specific and doesn't work. commandArgs(TRUE)["infile"] don't work both because of TRUE and of "infile". This one seems most close to the solution. I'll try to use setwd(dirname(rev(commandArgs())[1])). Carl, R converts file paths, indeed, but the slashes has to be doubled in the code (and it is the code after -e). – Arseny Apr 26 '12 at 00:51

2 Answers2

2

A couple of hints, made as someone who works on Windows by day, and Mac OS by night. I create my projects in a Dropbox folder which is common to both machines. I follow this work practice.

I use RStudio on both my machines. I start up RStudio by right-clicking on the script file locally and this sets the working directory to the file being opened. If I then keep all paths in my script relative, then I can share my projects with myself easily :)

I start my scripts by setting a global variable, in a line that looks silly:

DIR <- getwd()

and then I use relative paths throughout the rest of the scripts. with lines like this:

new.path <- paste(DIR, "rel-path", "to", "new", "file", sep="/")

This avoids my having to tinker with profiles on each machine. It does look obvious, but it gives me one place to change the DIR value in case I want the script to point elsewhere, say, in the morning

DIR <- "~/workspace/newproject"

or, in the evening,

DIR <- "c:/R_workspace/yet/a/different/project"

I also have to be careful that I use the same R version and packages as, in a few cases, that has led to hiccups a few times.

It is a simple flow, but effective and allows seamless working across the two systems

daedalus
  • 10,873
  • 5
  • 50
  • 71
0

Oops. Explorer starts R in the source directory if it is not on network folder. Initially I tried to start it on network folder. So the only thing to do is to copy the files to the local drive, or to map the network drive to a letter.

Arseny
  • 207
  • 2
  • 6