The working directory can be your best friend or ruthless adversary. Confusion about the working directory is a great time waster. Whenever you want to perform some advanced action, you need to ensure that you are in the correct directory. Otherwise, it can create an issue for you.
getwd in R
The getwd in r is a built-in function that returns the absolute filepath representing the current working directory of the R process. Let’s talk about the setwd() function in R Language.
setwd in R
The setwd in r is a built-in function that changes the current directory as a working directory. The setwd() method takes a new working directory as an argument. To change the current working directory in R, use the setwd() function.
The setwd() function is generally used to navigate the data directory.
The function takes dir as an argument, a character string: tilde expansion will be done.
The setwd() method returns the current directory before the change, invisibly and with the same conventions as the getwd() function. It will give an error if it does not succeed.
To change the current working directory, use the setwd() function. The setwd() function requires the new working directory as an argument to the function.
ls() character(0) getwd() setwd("/Users/krunal/Desktop/code/R/data") setwd("packageFiles") getwd()
The setwd() function is used to point at a particular directory.
In this example, we had to specify a character string containing the filepath we want to use within the setwd function. This is because you have to use a slash (not a backslash) to specify your path when working on a windows computer.
Set working directory in RStudio
For the first time you use R, the suggested procedure, under Windows and MAC OSX, is as follow:
- Create a sub-directory, say R, in your “Documents” folder. This sub-folder, also known as a working directory, will be used by R to read and save files.
- Launch R by double-clicking on the icon.
- Specify your working directory to R:
- On Windows: File –> Change directory
- On MAC OSX: Tools –> Change the working directory
Advance example of R setwd() Function
The setwd() function has uses beyond just set the current working directory. However, the following points allow you to iterate and execute a set of scripts.
- The setwd() function is used to point at a particular directory.
- The list.files() function is used to grab everything in the directory and subfolders.
- Then we use the pattern match to get the scripts we need.
- Use the source function to execute the R code.
Let’s write the code based on these scenarios.
# setwd() Function - To set the working directory your scripts folder setwd("/your scripts directory") # keep only the files that END with ".R" or ".r" using list.files() method and # pass the match parameter script_list <- list.files(pattern = ".*\\.[rR]$", recursive = TRUE) # loop through and trigger scripts using source () for (i in script_list) source(i)
Here, we are getting a list of files whose extension is .R.
First, we set a current working directory using the setwd() function and pass the path you want to set.
Then we are using the list.files() function, which accepts the pattern argument, which will return all the files recursively and make a list of them.
In the final step, we loop through that list one by one and trigger it using the source() function.
Also, each script is executing within its subfolder. This allows us to load/use different versions of any supporting scripts and data sets.
If you want to run an R script from a batch file to automate the same process and set a working directory in the batch file using the setwd() method.
R provides a function called setwd() which can help you to set the current directory.
R provides a function called getwd() which returns the working directory.
That is it for the setwd() function.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.