title: How to get the directory of the executing script in R?
body: <p>I have a function that looks like this:</p>
  
  <pre><code>read_data &lt;- function(filename, header) {
    path &lt;- paste("./output/", filename, sep = "")
    if (file.exists(path)) {
        data &lt;- read.csv(file = path, header = header, sep = ",")
    }
    # Partially removed for brevity.
  }
  </code></pre>
  
  <p>What I want to achieve is that, given a filename, I want to search whether that filename is available inside the <code>output</code> subdirectory, which is a subdirectory where my script is located, and if it is available, I want to read that file. The problem is that as long as I know <code>read.csv</code> function's file argument requires a full path for the file. So, I somehow need to get the directory where my script is located, so I can concatenate it with the rest of the subdirectory and filename. I can get the current working directory with <code>getwd()</code>, but that's not quite the same thing, as my working directory seems always to be fixed, whereas the script can be located anywhere in the computer. Any ideas how to get the directory of the script, and concatenate it with the output subdirectory and the provided filename in R?</p>