How to set the current file location as the default working directory in R programming?
<p>I want to make the current file location as the working directory.  </p>

<p><strong>Using Rstudio (Works!):</strong> </p>

<pre><code># Author  : Bhishan Poudel
# Program : writehere.r
# Source  : Rscript writehere.r

# set working directory here
this.dir &lt;- dirname(parent.frame(2)$ofile) # frame(3) also works.
setwd(this.dir)

# Sample data to test this code
mydata &lt;- seq(1:10)
write.csv(mydata,"writehere.dat")
#This works flawlessly in  MacOS 10.9 and Ubuntu 15.1.
</code></pre>

<p><strong>Using Command from terminal :   Rscript writehere.r (Does not work!)</strong>  </p>

<pre><code>Error in dirname(parent.frame(2)$ofile) : 
  a character vector argument expected
Execution halted


------------------
(program exited with code: 1)
</code></pre>

<p><strong>Using Command from terminal :   Rscript writehere.r (Works now!)</strong></p>

<pre><code># Author  : Bhishan Poudel
# Program : writehere.r
# Source  : Rscript example.r

# set working directory here
this_dir &lt;- function(directory)
setwd( file.path(getwd(), directory) )

# Sample data to test this code
mydata &lt;- seq(1:10)
write.csv(mydata,"writehere.dat")
</code></pre>

<p><strong>Using function inside ~/.Rprofile for Rstudio (Works!) :</strong>, </p>

<pre><code>##############################################
# inside ~/.Rprofile
# set up working directory
setwd_thisdir &lt;- function () {
  this.dir &lt;- dirname(parent.frame(3)$ofile)
  setwd(this.dir)
} 
##############################################
</code></pre>

<p>Then, in any directory let's say I have a file writehere.r, now it works.   </p>

<pre><code># Author  : Bhishan Poudel
# Program : writehere.r
# Compile : Rscript writehere.r

# set working directory here
setwd_thisdir

# Sample data to test this code
mydata &lt;- seq(1:10)
write.csv(mydata,"writehere.dat")
</code></pre>

<p><strong>Question:</strong>
Why the function </p>

<pre><code>this.dir &lt;- dirname(parent.frame(2)$ofile) # frame(3) also works.
setwd(this.dir)
</code></pre>

<p>does not work for text editors other than Rstudio?</p>

<p>Some useful links are following:<br>
<a href="https://stackoverflow.com/questions/34843889/r-setting-working-directory-to-source-file-location">R setting working directory to source file location?</a><br>
<a href="https://stackoverflow.com/questions/13672720/r-command-for-setting-working-directory-to-source-file-location?rq=1">R command for setting working directory to source file location</a><br>
 <a href="https://stackoverflow.com/questions/8835426/get-filename-and-path-of-sourced-file">get filename and path of `source`d file</a><br>
<a href="https://stackoverflow.com/questions/15026463/setwd-in-the-current-working-dir">setwd() in the current working dir</a><br>
<a href="https://stackoverflow.com/questions/26519172/command-for-set-working-directory-to-source-file-location?rq=1">Command for &quot;Set working directory to source file location&quot;</a><br>
<a href="https://stackoverflow.com/questions/29732597/sublimetext-and-r-setting-current-file-directory?rq=1">SublimeText and R: Setting Current File Directory</a><br>
<a href="https://stackoverflow.com/questions/23107094/setting-working-directory-through-a-function?rq=1">Setting working directory through a function</a><br>
<a href="https://stackoverflow.com/questions/31433559/what-is-a-fool-proof-way-of-permanently-setting-r-working-directory?rq=1">What is a fool-proof way of permanently setting R working directory?</a><br>
<a href="https://stackoverflow.com/questions/34843889/r-setting-working-directory-to-source-file-location?rq=1">R setting working directory to source file location?</a><br>
<a href="https://stackoverflow.com/questions/35523730/how-to-get-into-the-directory-of-a-file-in-r?rq=1">How to get into the directory of a file in R?</a></p>