Getting familiar with the working environment: Getting familiar with the R interface
Working with working directories
The working directory is the default location on your computer's file system where R will look for and save files unless you specify otherwise. When you read or write files, such as data files or scripts, R will use the working directory as the starting point to find or store these files.
You can check the current working directory in R via the getwd()
function. Run getwd()
in the R Console, and it will display the location of the current working directory on your computer.
To define another working directory (i.e. a folder you want R to save your files to and look for files you want to import), you can use the RStudio interface or you can use R code.
- Go to the Session menu at the top.
- Select Set Working Directory and then choose one of the following options:
- To Source File Location: this sets the working directory to the location of the currently active R script.
- To Choose Directory...: this opens a dialog box in which you can manually select the desired working directory though the file browser.
you can use the setwd()
function of R to set the working directory programmatically. The setwd()
function takes the path to the desired directory as an argument. For example, if you want to set the working directory to C:/Users/YourUsername/Documents/R_Projects, you would use the following R code:
setwd("C:/Users/YourUsername/Documents/R_Projects")