It is convenient to be able to set the working directory of a script to its parent directory. This allows you to point to the relative path of files associated with it. For example, if your working directory is set to the location of init.sh, then you will be able to read in data/file.dat without specifying its full path. If these files are in a git repo - you can also be assured they will travel together.

├── init.sh
└── data
    └── file.dat

In bash you can set the directory to the location of the script being executed using:

cd "${0%/*}"

# Or more obviously
cd "$(dirname "${BASH_SOURCE[0]}")"

In python you can set the directory to the location of the script being executed using:

import os
from os.path import dirname, abspath

os.chdir(dirname(abspath(__file__)))

In R, unfortunately, no straightforward method exists. This stackoverflow question has some useful answers though. Two are listed below, and I’ve added a third option for setting the work directory relative to a git repo.

setwd to script location when calling Rscript

getScriptPath <- function(){
    cmd.args <- commandArgs()
    m <- regexpr("(?<=^--file=).+", cmd.args, perl=TRUE)
    script.dir <- dirname(regmatches(cmd.args, m))
    if(length(script.dir) == 0) stop("can't determine script dir: please call the script with Rscript")
    if(length(script.dir) > 1) stop("can't determine script dir: more than one '--file' argument detected")
    return(script.dir)
}

# Setting the script path would then be:
setwd(getScriptPath())

setwd to script location in Rstudio

# This will not throw an error if you are not using rstudio.
try(setwd(dirname(rstudioapi::getActiveDocumentContext()$path)))

Setting the script path relative to the git repo

Most of my code resides in git repositories - so an alternative to setting the working directory to the location of a script is to set it to the location of a git repository. Here is how I do that in R:

# First set the working directory to the location of a script (useful for working in Rstudio)
try(setwd(dirname(rstudioapi::getActiveDocumentContext()$path)))
# Next, set the directory relative to the git repo
setwd(system("git rev-parse --show-toplevel", intern=T))

The big advantage of this approach is that you can call the script from anywhere while you are located in its git repo, and it will always execute from the base or top-level of that repo.