R always points a default working directory. You will learn what is your working directory, how to change working directory and how to clear working directory in R or RStudio.

In this article, you will learn the answers of the following questions.

  • What is current working directory in R?
  • How do I change my working directory in R?
  • Why can’t I change my working directory in R?
  • How do I clear my working directory in R?

Changing the working directory is essential for R users to specify the folder in which they work. Firstly, you will learn what is your current directory in R. Secondly, you will learn how to change working directory in r. Sometimes, it may be challenging to change working directory in R. Therefore, possible reasons why the working directory can not be changed are discussed. Last, you will learn how to clear the working directory in R.

You can create a directory of R in your folder. For this purpose, you need to know the filepath of your folder. Before the change, getwd() shows the current directory. After that, you can use setwd() to change your working directory in R.

Check Out: How to Download and Install R for Windows

What is Current Working Directory in R?

You can see your current working directory by coding getwd (get working directory) function. getwd() function has no arguments. Therefore, just type it in R console. It returns an absolute filepath representing the current working directory of the R process.

getwd()
## [1] "C:/Users/DataScience/Desktop"

Also Check: What are Data Types in R?

How to Change Working Directory in R

You can change your working directory with setwd (set working directory) function. The setwd() function includes an argument, a filepath of the desired working directory. Thus, I write the filepath of my working directory in setwd() function with double-quote to set my curent working directory to DataScience file in D drive.

setwd("D:/DataScience")

Then, you can call your functions from your working directory. Before you call your functions, you can list the files in your working directory.

list.files()
## [1] "gplot"

Also, you can write your output/graphics to this directory. For example, I call gplot function from the working directory.

source("gplot.R")

You can see the objects available in R console. For this purpose, just type ls() function in your R console.

ls()
## [1] "gplot"

Sometimes, you can not change working directory in R. The most probable reason of this is misspelling path of the file you want to work in. Also, you need to write the filepath in double-quote. Otherwise, you can not change working directory.

How to Clear Working Directory in R

After you set working directory, you may want to clear your working directory in R. For this purpose, we can use unlink() function.

unlink("D:/DataScience", recursive = TRUE)

The application of the codes is available in our youtube channel below.

How to Change Working Directory in R
Subscribe to YouTube Channel

Don’t forget to check: How to Import Data into R


Dr. Osman Dag