[R] How to "source" a R script in a parent/parallel directory (win/linux)

ottorino ottorino-luca.pantani at unifi.it
Tue Jun 11 10:09:21 CEST 2013

Dear R users,
I would like to source a file independently from the operating system,
but I cannot figure out how.
I apologize for the verbosity of this mail, 
but English is not my mother tongue, so I cannot be concise and precise
as I can be in my own language.

I'm writing a script which will be run by some people who are not
familiar with R.
I already tried to teach them some rudiments, but unsuccessfully.
They are only willing to double click an icon, and do not want to bother
with many details.

Consider the following situation, in where I have to run the very same
script under Linux (for debugging) and under windows (for the users)

On Win

On my Linux box the same path is a samba share mounted on

The file I would like to run (say, RunThis.R) with a "source" command
will be located at


This location will never change, while "user1" will change each time,
depending on the user or on the data to be processed. In other words
each user will have his/her workspace under different and parallel

RunThis.R needs also some files and script which will be located under 

My intent here is not to allow the users to modify RunThis.R and the
file under Constants_and_Functions.
I already tried this but you probably alredy imagine which were the

Under each "user?" there will be two dir, one with the raw data to be
processed and another with the outcome of the analysis and the processed

The unfamiliar "user?" will have to modify ONLY a small script (say, C:\
\mypath\\user1\\options.R, C:\\mypath\\user2\\options.R )in which he/she
can modify some options (jpeg or pdf, in English or in Italian, csv or
xls and so on.)

I've found this solution to the problem, but I wonder if there are some
more elegant alternatives.
Each options.R file would finish with lines like the following

if(Sys.info()[1] == "Linux"){
Fun.Const.Dir <-
source(file.path("/mnt/mymountpt/mypath/PROGRAM", "RunThis.R"))
} else {
Fun.Const.Dir <-
    file.path("C:\\mypath\\PROGRAM", "Constants_and_Functions")
source(file.path("C:\\mypath\\PROGRAM", ""RunThis.R"))

My idea was that in R would have been possible to write something
similar to what I write in latex with figures, i.e.

so something like 
source("../PROGRAM/RunThis.R") would have been worked. but it didn't

I tried a brute attempt like the following: 
step.back <- setwd("..")
source(file.path(step.back, ""RunThis.R"))

but without success.

Any suggestion ?

Ottorino-Luca Pantani, Università di Firenze
Dip.to di Scienze delle Produzioni Agroalimentari e  
dell'Ambiente (DISPAA)
P.zle Cascine 28 50144 Firenze Italia
Debian 7.0 wheezy -- GNOME 3.4.2
GNU Emacs (i486-pc-linux-gnu, GTK+ Version 3.4.2)
ESS version 12.04-4 -- R 2.15.1

More information about the R-help mailing list