How to change Working Directory in R

When we open RStudio or R gui it uses Default Working Directory.

getwd(): It is used to know the current working directory
setwd(): It is used to change the current working directory to another.

When RStudio started following things will happen:

Executes the .Rprofile (if any) from the default working directory.
Loads the .RData file (if any) from the default working directory into the workspace.

When RStudio exits: There are changes to the workspace, a prompt asks whether these changes should be saved to the .RData file in the current working directory.

How to change Working Directory in R

We can change the current working directory using setwd() command or GUI method as show below.

Using setwd() function:

> getwd() #To get current working directory [1] "C:/Users/pinnapav/Desktop/R_Practice"

> setwd("C:/R Files") #To set new directory as current working directory > getwd() [1] "C:/R Files"

Using RStudio(GUI method):

1. Open RStudio–>Tools–> Select Global Options.

How to change Working Directory in R

2. Then browse to new directory as shown below.

3. Click on Apply.

4. Restart RStudio, to affect these changes.

5. Then, run getwd() for confirmation

> getwd() [1] "C:/R Files"

CheapSexCams
Responses are currently closed, but you can trackback from your own site.

Comments are closed.

Powered by k2schools
%d bloggers like this: