How to set a working directory in R

In R, you can set a working directory in this way (I use C:/temp as an example).

setwd("C:/temp")

If this generates an error message. suspect that you used a wrong quotation mark (this can happen when you copy and paste an example from the Internet).  Just retype using '.  You can read more about it here.

You also need to be careful about the slash.  Microsoft Windows uses backward slashes, "\", to indicate the folder structure.  R uses forward slash, "/" at least on my machine.  This may be OS-dependent, so to really determine which slash is used on your environment, try this command to know the currently working directory and see which is used:

getwd()

I got this on my personal PC ("/" is used).

"C:/Users/kaz/Documents"

Alternatively, go to File and click on Change dir... to specify the working directly.  (I don't see this option in R-Studio), so this must be specific to the regular version of R).

 

CategoriesR

Leave a Reply