Get and Set Working Directory in R
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:
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
print ( "Current working directory" )
getwd ()
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
setwd ( "/Users/mallikagupta/Desktop" )
setwd ( "GFG" )
print ( "Current working directory" )
getwd ()
|
Output
[1] "Current working directory"
[1] "/Users/mallikagupta/Desktop/GFG"
Last Updated :
30 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...