Get and Set working directory in R

In This chapter we will learn how to

  • Get current working directory in R
  • Set working directory in R

Set working Directory in R:

If you want to read files from a specific location or write files to a specific location, you need to set working directory in R. This can be accomplished by specifying path with in setwd() function. The following example shows how to set the working directory in R.

# set working directory in R

setwd("D:/ LearningR”)

which sets working directory to LearningR folder in D drive.

Get working directory in R:

R will always be pointing to a directory on your computer. You can find out which directory by running the getwd() function. The following example shows how to get the working directory in R

# get current working directory in R

getwd()

Output will be current working directory

“D:/LearningR”

 

Set Working directory in RStudio through GUI(Graphical user interface):

In R studio we can set working directory in 3 steps through GUI.
Step 1: on Right side down quadrant, under files tab click … as shown in the red box below

set working directory in r 1

Step 2:  It will pop up the navigation box, through which you can navigate to the desired location you want and click ok button

set working directory in r 2

Step 3: now under file tab click More > Set As Working Directory as shown below

set working directory in r 3

Now the working directory will be set. Thus we can set working directory in Rstudio through GUI.

previous small get and set working directory in R                                                                                                                next small set working directory in R