Skip to content
Related Articles

Related Articles

Improve Article

Get and Set Working Directory in R

  • Last Updated : 30 Jun, 2021

In this article, we are going to see how to get and set a working directory in R Programming Language.

How to Get Working directory:

getwd(): The getwd() method is used to gather information about the current working pathname or default working directory. This function has no arguments. It returns an absolute pathname. It returns NULL in case there is not any working directory. 

getwd()

Code:

R




# getting the working directory
getwd()

Output



[1] "/Users/mallikagupta/Desktop"

How to set up a working directory:

setwd(): This method is used to set the specified pathname as the current working space directory of the R console. 

Syntax: setwd(dir) 

Where, dir is specify a working directory. 

The setwd() function allows us to move a hierarchy level up in the folder domain using the “..” argument in this method. 

Code:

R




# getting the working directory
print ("Current working directory")
getwd()
  
# set working directory to its parents directory
setwd("..")
print ("Modified working directory")
getwd()

Output

[1] "Current working directory"
[1] "/Users/mallikagupta/Desktop" 
[1] "Modified working directory" 
[1] "/Users/mallikagupta"

The subfolders can be easily accessed once the root directory has been set or is available by further specifying the character name of the sub-directory as a character string in the function arguments. 

R




# setting parent directory
setwd("/Users/mallikagupta/Desktop")
  
# adding sub directory
setwd("GFG")
print ("Current working directory")
  
# getting the working directory
getwd()

Output

[1] "Current working directory" 
[1] "/Users/mallikagupta/Desktop/GFG"



My Personal Notes arrow_drop_up
Recommended Articles
Page :