Open In App

How to Fix in R: Cannot change working directory

In this article, we focus on how we can fix the “cannot change working directory” error in the R programming language.

One error that one may encounter in R is:



Error in setwd("C:/Bhuwanesh/gfg") :
 cannot change working directory

Such an error occurs when we try to set a working directory in R, but a part of the file path is misspelled. 

When this error might occur:

Let’s try to set the following working directory in R. 



Example:




# Try to move to the working directory
# C:/Bhuwanesh/gfg
setwd("C:/Bhuwanesh/gfg")

Output:

We received the above error because there is no folder by the name of gfg in our local system.

Reasons for the occurrence of this error:

There can be a lot of reasons for which such an error might occur in R. Some of the reasons are the following:

Fixing:

We can fix the error easily by altering the wrong file path to the correct file path.

Example:      




# Try to move to the working directory
# C:/Bhuwanesh/GeeksforGeeks/
setwd("C:/Bhuwanesh/GeeksforGeeks/")

Output:

Output

This time we didn’t receive any error as the R compiler was successfully able to set the working directory. In order to make sure that the working directory has been changed successfully we can use the getwd() function to get the status of the current working directory.




# Display the path of the current
# working directory
getwd()

Output:

Output


Article Tags :