Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the current file location as the default working directory in R programming?

Tags:

I want to make the current file location as the working directory.

Using Rstudio (Works!):

# Author  : Bhishan Poudel # Program : writehere.r # Source  : Rscript writehere.r  # set working directory here this.dir <- dirname(parent.frame(2)$ofile) # frame(3) also works. setwd(this.dir)  # Sample data to test this code mydata <- seq(1:10) write.csv(mydata,"writehere.dat") #This works flawlessly in  MacOS 10.9 and Ubuntu 15.1. 

Using Command from terminal : Rscript writehere.r (Does not work!)

Error in dirname(parent.frame(2)$ofile) :    a character vector argument expected Execution halted   ------------------ (program exited with code: 1) 

Using Command from terminal : Rscript writehere.r (Works now!)

# Author  : Bhishan Poudel # Program : writehere.r # Source  : Rscript example.r  # set working directory here this_dir <- function(directory) setwd( file.path(getwd(), directory) )  # Sample data to test this code mydata <- seq(1:10) write.csv(mydata,"writehere.dat") 

Using function inside ~/.Rprofile for Rstudio (Works!) :,

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

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

# Author  : Bhishan Poudel # Program : writehere.r # Compile : Rscript writehere.r  # set working directory here setwd_thisdir  # Sample data to test this code mydata <- seq(1:10) write.csv(mydata,"writehere.dat") 

Question: Why the function

this.dir <- dirname(parent.frame(2)$ofile) # frame(3) also works. setwd(this.dir) 

does not work for text editors other than Rstudio?

Some useful links are following:
R setting working directory to source file location?
R command for setting working directory to source file location
get filename and path of `source`d file
setwd() in the current working dir
Command for "Set working directory to source file location"
SublimeText and R: Setting Current File Directory
Setting working directory through a function
What is a fool-proof way of permanently setting R working directory?
R setting working directory to source file location?
How to get into the directory of a file in R?

like image 506
BhishanPoudel Avatar asked Jan 29 '16 00:01

BhishanPoudel


People also ask

Which command is used to change the current working directory of R?

The cd newDir command will change the current working directory.

What does it mean to set working directory in R?

The working directory is just a file path on your computer that sets the default location of any files you read into R, or save out of R. In other words, a working directory is like a little flag somewhere on your computer which is tied to a specific analysis project.

Which of the following command is used to display the current working directory of R?

The pwd command is used to display the current working directory.


1 Answers

Simply, use rstudio API, extract its directory, and set it as a working directory as shown below:

setwd(dirname(rstudioapi::getSourceEditorContext()$path)) 

Verify if you set the directory correctly by the following command:

getwd() 
like image 127
Suresh Gautam Avatar answered Sep 23 '22 04:09

Suresh Gautam