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:

 

 

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.


26 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
    • Hi Nicole,

      What is your operating system? Are you working with Windows or MAC?

      Regards,
      Joachim

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

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

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

      Reply
  • Hi There,

    just a random tip that worked for me was to put double backslash.

    Thanks

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

    Reply
  • setwd(“C:/Users/acer/Downloads/Data/Data/census_income”)
    Error in setwd(“C:/Users/acer/Downloads/Data/Data/census_income”) :
    cannot change working directory

    Reply
    • Hello Jay,

      This can happen for a few reasons:

      Path does not exist: The directory you’re trying to switch to doesn’t exist. Double-check the path for any typos or errors. Ensure that all the folders in the path (C:/Users/acer/Downloads/Data/Data/census_income) actually exist.

      Permission issues: You might not have the necessary permissions to access the directory. This can happen if the folder has restricted permissions. Try accessing the folder outside of R (e.g., using a file explorer) to see if you can open it without any issues.

      Invalid characters: Ensure that you’re using regular quotation marks (“) around the path. In your command, you’ve used curly quotation marks (“”), which can sometimes happen when copying text from certain word processors or websites. R might not recognize these as valid string delimiters.

      R session issues: Sometimes, an issue with the R session itself can cause problems. Try restarting R and running the command again.

      Best,
      Cansu

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Top