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:

 

The YouTube video will be added soon.

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


14 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”

    Reply
    • 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

      Reply
  • As always only very basic and not useful information. Thank you for nothing!

    Reply
    • 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

      Reply
  • 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

    Reply
    • 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

      Reply
  • Do you have any advice on this error when referencing a server path/file?

    Reply
  • 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.

    Reply
  • 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

    Reply
    • 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

      Reply
  • 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

    Reply

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top