R Error in setwd() : cannot change working directory (2 Examples)
In this article you’ll learn how to deal with the “Error in setwd() : cannot change working directory” in the R programming language.
Table of contents:
Let’s take a look at some R codes in action:
Example 1: Reproduce the Error in setwd() : cannot change working directory
In this section, I’ll explain how to replicate the error message “cannot change working directory” in R.
Let’s assume that we want to set our working directory to a folder called “Some Folder” on our Desktop using the setwd function.
Then, we might try to do that as shown below:
setwd("C:/Users/Joach/Desktop/Some Folder") # Try to use non-existing folder # Error in setwd("C:/Users/Joach/Desktop/Some Folder") : # cannot change working directory
Unfortunately, the RStudio console returns the error message “cannot change working directory”.
The reason for this is that the directory we are trying to access does not exist. We might have specified the folder name wrong, or the path before the folder name is not existing.
Example 2: Fix the Error in setwd() : cannot change working directory
We can solve this problem by specifying the path correctly. In this example, the problem was that the folder “Some Folder” does not exist on my Desktop. For that reason, I can avoid the “Error in setwd() : cannot change working directory” by setting the working directory directly to my Desktop:
setwd("C:/Users/Joach/Desktop/") # Properly specify setwd
Executing the previous R code does not lead to any errors.
Video, Further Resources & Summary
Do you need more information on the R code of this post? Then you may want to watch the following video tutorial of my YouTube channel. In the video, I illustrate the R code of this tutorial:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you may read some of the other articles of this homepage.
You have learned in this tutorial how to avoid the “Error in setwd() : cannot change working directory” in the R programming language. In case you have any additional questions, let me know in the comments.
24 Comments. Leave new
Hi,
I am getting the ‘Error in setwd() : cannot change working directory”. As per your advise, I tried fixing tge path as suggested. On trying to read the csv file I am trying to load, I now get the error message,’ could not find function “read_csv”
Hey Ben,
I’m sorry for the delayed response, I just came back from vacation. Do you still need help with your error?
Regards,
Joachim
As always only very basic and not useful information. Thank you for nothing!
You could share your code and explain your problem in some more detail, instead of just being rude. I’m happy to help.
Regards,
Joachim
Hi Joachim,
I can’t change directory using setwd(). My path is D:/GoogleAnalyticsCertificate/Capstone Project/Fitabase Data 4.12.16-5.12.16/. Thank you for your help.
Error is as shown below.
> setwd(“D:/GoogleAnalyticsCertificate/Capstone Project/Fitabase Data 4.12.16-5.12.16/”)
Error in setwd(“D:/GoogleAnalyticsCertificate/Capstone Project/Fitabase Data 4.12.16-5.12.16/”) :
cannot change working directory
Hey Pete,
Your code seems to be correct, so I assume there must be some spelling errors in your path. Are you certain that you have spelled the folder names etc. correctly?
Regards,
Joachim
Do you have any advice on this error when referencing a server path/file?
Hey Brooke,
Could you please share the code that returns this error?
Regards,
Joachim
Hey, Joachim
I’m trying to change the directory on my android phone in the “R compiler”. Actually, I’ve no clue how to write the right pass to the needed folder ( the primary storage/r/lab1)
I’ll be crazy grateful, if u can help me with this.
Best regards,
Susan.
Hey Susan,
Unfortunately, I have never done this myself. However, I have recently created a Facebook discussion group where people can ask questions about R programming and statistics. Could you post your question there? This way, others can contribute/read as well: https://www.facebook.com/groups/statisticsglobe
Regards,
Joachim
Hello
I am using r-studio on Fedora36. My problem is with Bioconductor packages it seems because most packages upon installation throws me the “unable to write to path” and “can’t update packages” error messages. But the packages that it lists are not the package i want to install (maybe prerequisites). Now i tried to start rstudio from the terminal with sudo and the problem ofc doesn’t occur it can overwrite anything it want. But the first problem with this workaround is that working directory is locked (/root) (at least i cannot seem to change it).
If you have any thoughts on this thanks in advance. Probably i am missing a very simple solution.
Best regards
Hi John,
Unfortunately, I don’t have experience with Fedora36. However, I have recently created a Facebook discussion group where people can ask questions about R programming and statistics. Could you post your question there? This way, others can contribute/read as well, and maybe somebody in the group knows a solution: https://www.facebook.com/groups/statisticsglobe
Regards,
Joachim
Hi – I am new to R and coding in general. I am receiving an error like the one below.
Error in setwd(“/Users/nicolenappi/Desktop/Harris Autumn 2022/Stats/Mini Assignments/2”) :
cannot change working directory
Below is my file path
#/Users/nicolenappi/Desktop/Harris Autumn 2022/Stats/Mini Assignments/2
Hi Nicole,
What is your operating system? Are you working with Windows or MAC?
Regards,
Joachim
am using MAC and I keep getting the same error as below when trying to change directory.
setwd(“/Users/gasnaken/Desktop/Capstone P”)
Error in setwd(“/Users/gasnaken/Desktop/Capstone P”) :
cannot change working directory
Hey Moses,
Please excuse the late response. I was on a long holiday so unfortunately I wasn’t able to reply sooner. Still need help with your code?
Regards,
Joachim
I do, I’m having the same issue as them and am on mac.
My code is setwd(“/Users/jaiden/Desktop/CoffeeOrder”)
My path file is the same Users/jaiden/Desktop/CoffeeOrder
Hello Jaiden,
Sorry for the late response. Do you still need help?
Regards,
Cansu
setwd(“C:/Users/Joach/Desktop/”) # Properly specify setwd
Error in setwd(“C:/Users/Joach/Desktop/”) :
cannot change working directory
Hello,
As you have suggested to use this code : setwd(“C:/Users/Joach/Desktop/”) # Properly specify setwd
to solve the error (file, “rt”)
I just get the above error instead
Error in setwd(“C:/Users/Joach/Desktop/”) :
cannot change working directory
Can you help me out with that?
Thank you in advance
Hi Jakleen,
“C:/Users/Joach/Desktop/” is a placeholder for a path that works on my computer. You would have to insert your own file path instead. You can learn more about file paths here.
Regards,
Joachim
Hi There,
just a random tip that worked for me was to put double backslash.
Thanks
Hello Guy,
Thank you for your contribution.
Regards,
Cansu
Hi,
I‘m new to R and coding in general. I tried to fix it, but it still shows the same error.
Error in setwd(“R_Start“): cannot change working directory
(My folder is called R_Start)
I‘m working with Mac and I hope you can help me.
Thank you.
Regards,
Amina
Hello Amina,
Looks like you don’t have a proper file path. Please see our tutorial about setting working directory. The path should be seemed like there.
Best,
Cansu