Open In App

How to Use setwd and getwd in R?

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to use setwd and getwd in the R programming language.

getwd() function

getwd() stands forget working directory. It is used to get the current working directory of the environment.

Syntax:

getwd()

We can also see the total number of files in the present working directory. For that, we have to use the length function.

Syntax:

length(list.files())

If we have to use the display the filenames, then the command is 

list.files()

Example :

In this example, we will be using the getwd() function to get the current working directory.

R




getwd()


Output:

"C:/Users/Ramu/saisri"

Example :

Here, we will be using the getwd() function to get the length of the list of the files present in the working directory of the R console.

R




# get total file count
print(length(list.files()))
  
# get file names
print(list.files())


Output:

[1] 2
[1] "ai.R"   "ramu.R"

setwd() function

setwd() stands for set working directory. This is used to set the working environment.

Syntax:

setwd('path')

Example:

Here, we will be using the setwd() function to set the working directory.

R




setwd('C:/Ramu/saisri/')




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads