Setting a working directory: "Error in setwd: cannot change working directory" under "C:/Documents and Settings/..."

Vanessa M picture Vanessa M · May 8, 2018 · Viewed 13k times · Source

I was wondering why it won't let me change my working directory.
I keep seeing this error:

Error in setwd(dataDir) : cannot change working directory

This is my code:

dataDir <- "C:/Documents and Settings/My Documents/R/"
setwd(dataDir)

Answer

smci picture smci · May 8, 2018

Seems to be an annoying Windows permissions issue; that's a legacy directory. There are 2.93 million Google hits along the lines of "Windows (7/)10: Access to the path 'C:\Documents and Settings' is denied". Look for advice in Windows forums, and check what the ownership and permissions on "C:/Documents and Settings/My Documents/..." are currently, and what they're supposed to be, and whether Administrator account is enabled. (Frankly not the best choice for the R install packages directory, but many installers use it, or default to it.)

  • Please confirm that setwd() works fine for a more harmless choice of directory, e.g. 'C:\Users\your_name\R'
  • Are you running R as Administrator or User?
  • Did you install R as Administrator or User? (which installer did you use, and which installer version/date? R-project's? Anaconda? another?)