Where's the script?

Althogh I wrote some automatically running R scripts, setting working directories always caused me a headache; unless I called Rscript from the right directory (which I coundn't do with cron), all script started with
This was fine untill I had to share the code with other participants, as 1.: it wouldn't run on their computer, and 2.: it exposed the path on my computer, which I found disturbing. So, I came up with a solution.

The commandArgs function returns all the arguments of the running Rscript. Among them, there is the currently used .R file: "--file=running_script.R". I just had to get rid of the --file= part and give the result to dirname to get the answer to the original question. Here's the whole function as a gist:

Now I start every script with