0

I'm currently trying to Knit a file with RStudio and keep running into the below error:

Error in setwd("D:/") : cannot change working directory
Execution halted

However, when I check getwd() I get "/Users/my-name" as the working directory. I have also tried setwd("~/") and receive the same error in Markdown. Further, when I initially open RStudio I get

Error in setwd("D:/") : cannot change working directory

which suggests that there is an issue when I start R. Is there a way to get this error to stop from occurring when starting RStudio in the first place? I've tried deleting and redownloading RStudio but that doesn't seem to work. I'm currently using Mac OS Catalina.

Below is a screenshot of the error I receive when I open RStudio.

Screenshot

Thanks in advance.

AW27
  • 481
  • 3
  • 15
  • You should change the working directory like this: ```{r setup} knitr::opts_knit$set(root.dir = '/tmp') ``` as described here: https://stackoverflow.com/questions/20060518/in-rstudio-rmarkdown-how-to-setwd – Tea Tree Jul 14 '20 at 18:34
  • It looks like I still get ```Error in setwd("D:/") : cannot change working directory Execution halted``` when trying to run ```Knit```. I'm also wondering if there is a way to stop the error from occurring when I initially open ```R```. – AW27 Jul 14 '20 at 18:36
  • If you get "/Users/my-name" when you run `setwd()` that would imply that you are on a Mac. But the a path like "D:\" is something you would use on Windows. There's no such thing as a "D:\" drive on Mac. Is there code on your markdown file that tries to set the working directory explicitly? What exactly is in the file you are tying to knit. It would be helpful to see a proper [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Jul 14 '20 at 18:39
  • @MrFlick I've added a screenshot of the error when opening ```RStudio```. In terms of reproducible example, I am just trying to Knit the file initially and I think the error somehow relates to the screenshot but I don't know how that happened. – AW27 Jul 14 '20 at 18:44
  • 2
    Do you have an `.Rprofile` file that automatically runs code at Startup? Does `Sys.getenv("R_PROFILE_USER")` return anything? – MrFlick Jul 14 '20 at 18:47
  • You were right @MrFlick. I had a ```.Rprofile``` file in my working directory. Thanks for your help. – AW27 Jul 14 '20 at 18:57

1 Answers1

0

I got similar issue with "setwd". When I did "save as" my existing code file from other folder to a new folder then I got an error: "cannot change working directory". When I did copy a code file from other folder and paste it into a new folder then I was able to run "setwd" and there was no error.

DBhatta
  • 51
  • 7