1

I got some example codes from a course that start with

install.packages('maps')
install.packages('geosphere')
library(maps)
library(geosphere)
setwd("C:\\mydata")
...

Everything was fine until the command of setting working directory. Because I installed RStudio and R in D drive, I have also tried "C://", "C:/", "C:\", "D:\", "D:\", "D://", and "D:/". However, the console kept saying "Error in setwd( ): cannot change working directory".

This is probably a studpid question. But I'm a beginner in R, so could somebody be so kind to help me out? Thank you very much!

Paw in Data
  • 1,262
  • 2
  • 14
  • 32
  • Have you tried the graphical way from within `RStudio`? Something like `Ctrl + Shift+ H` or `Session - > Set Working Directory -> Choose Directory`? – deepseefan Nov 16 '19 at 23:57
  • Most simple case I can think of is when the directory doesn't exist already. – Hong Nov 16 '19 at 23:59
  • Aside from the error, starting R script with `setwd()` is not the best practice: I learned this from [link](https://www.tidyverse.org/blog/2017/12/workflow-vs-script/) – Hong Nov 17 '19 at 00:03
  • FYI, using `setwd` in R scripts is a bad coding practice. Use explicit paths as variables. – slava-kohut Nov 17 '19 at 02:41
  • Backslashes need to be escaped with backslashes on Windows machines so "C:\\\\mydata" might work *IF* mydata already exists on your machine. Also you can try `setwd(choose.dir())` followed by `getwd()` to get the full path to the directory you selected. But just because the examples used C:\\mydata doesn't mean you have to. Whichever way you go, you can use `dir.create()` to create a new directory/folder for your R work. – dcarlson Nov 17 '19 at 04:02
  • Thank you, everybody! I tried everything you have suggested and some of them seemed to work! ^_^ – Paw in Data Nov 18 '19 at 21:18

0 Answers0