Arseny
Arseny

Reputation: 205

Starting R in the source directory using Windows explorer

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?

Upvotes: 1

Views: 747

Answers (2)

Arseny
Arseny

Reputation: 205

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.

Upvotes: 0

daedalus
daedalus

Reputation: 10923

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

Upvotes: 2

Related Questions