title: Get the path of current script
body: <p>I would like to set the working directory to the path of current script programmatically but first I need to get the path of current script.</p>
  
  <p>So I would like to be able to do: <br></p>
  
  <pre><code>current_path = ...retrieve the path of current script ...
  setwd(current_path) 
  </code></pre>
  
  <p>Just like the RStudio menu does:
  <a href="https://i.stack.imgur.com/2Gxf0.png" rel="noreferrer"><img src="https://i.stack.imgur.com/2Gxf0.png" alt="RStudio set working directory"></a></p>
  
  <p>So far I tried:</p>
  
  <pre><code>initial.options &lt;- commandArgs(trailingOnly = FALSE)
  file.arg.name &lt;- "--file="
  script.name &lt;- sub(file.arg.name, "", initial.options[grep(file.arg.name, initial.options)])
  script.basename &lt;- dirname(script.name)
  </code></pre>
  
  <blockquote>
    <p>script.name <strong>returns NULL</strong></p>
  </blockquote>
  
  <pre><code>source("script.R", chdir = TRUE)
  </code></pre>
  
  <blockquote>
    <p><strong>Returns</strong>:
    Error in file(filename, "r", encoding = encoding) : cannot open the
    connection In addition: Warning message: In file(filename, "r",
    encoding = encoding) : cannot open file '/script.R': No such file or
    directory</p>
  </blockquote>
  
  <pre><code>dirname(parent.frame(2)$ofile)
  </code></pre>
  
  <blockquote>
    <p><strong>Returns</strong>: Error in dirname(parent.frame(2)$ofile) : a character vector argument expected
    <em>...because parent.frame is null</em></p>
  </blockquote>
  
  <pre><code>frame_files &lt;- lapply(sys.frames(), function(x) x$ofile)
  frame_files &lt;- Filter(Negate(is.null), frame_files)
  PATH &lt;- dirname(frame_files[[length(frame_files)]])
  </code></pre>
  
  <blockquote>
    <p><strong>Returns</strong>: Null because frame_files is a list of 0</p>
  </blockquote>
  
  <pre><code>thisFile &lt;- function() {
      cmdArgs &lt;- commandArgs(trailingOnly = FALSE)
      needle &lt;- "--file="
      match &lt;- grep(needle, cmdArgs)
      if (length(match) &gt; 0) {
          # Rscript
          return(normalizePath(sub(needle, "", cmdArgs[match])))
      } else {
          # 'source'd via R console
          return(normalizePath(sys.frames()[[1]]$ofile))
      }
  }
  </code></pre>
  
  <blockquote>
    <p><strong>Returns</strong>: Error in path.expand(path) : invalid 'path' argument </p>
  </blockquote>
  
  <p>Also I saw all answers from <a href="https://stackoverflow.com/questions/3452086/getting-path-of-an-r-script">here</a>, <a href="https://stackoverflow.com/questions/1815606/rscript-determine-path-of-the-executing-script">here</a>, <a href="https://stackoverflow.com/questions/26519172/command-for-set-working-directory-to-source-file-location">here</a> and <a href="https://stackoverflow.com/questions/28550711/r-set-working-directory-to-folder">here</a>.
  No joy.</p>
  
  <p>Working with <code>RStudio 1.1.383</code></p>
  
  <blockquote>
    <p><strong>EDIT:</strong> It would be great if there was no need for an external library to achieve this.</p>
  </blockquote>