Open In App

How to Fix in R: Cannot change working directory

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:

R




# 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:

  • When the file path name is misspelled.
  • Invalid characters are used in the file path.
  • We do not have permission to access a particular file path.
  • File exists but there are some other restrictions that exist for compilers.

Fixing:

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

Example:      

R




# 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.

R




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


Output:

Output



Last Updated : 18 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads