DEV Community

Dr. Azad Rasul
Dr. Azad Rasul

Posted on • Updated on

6- Paths and working directories in R programming

Set a default working directory in RStudio

Tools> Global Options> General> Default working directory (when not in a project)> Browse> Choose your directory> Apply> OK

Default_working_directory.png

Default_working_directory2.png

Get current directory

getwd() # stands for "get working directory"
Enter fullscreen mode Exit fullscreen mode

Change working directory

setwd("D:/R4Researchers")# setwd() function stands for “set working directory”
setwd('C:/Users/Azad/Downloads') # Change working directory to download folder
getwd()
Enter fullscreen mode Exit fullscreen mode

Choose directory from RStudio:

ctrl+shift+H
or: Session> Set Working directories> Choose Directory

List functions

ls() # ls() function shows defined data sets and functions
Enter fullscreen mode Exit fullscreen mode

ls().png

To remove everything in the working environment (only apply it when it is necessary):

rm(list=ls())
Enter fullscreen mode Exit fullscreen mode

Select path:

file.path('D:', 'Natural_Resources', 'Soil')
file.path('D:', 'R4Researchers', 'India_China_LAI_and_LST.csv')
Enter fullscreen mode Exit fullscreen mode

Use path for set directory

setwd(file.path('D:', 'Natural_Resources', 'Soil'))
getwd()
Enter fullscreen mode Exit fullscreen mode

An absolute path

dt<- read.csv('D:/R4Researchers/data_ziped/data_ziped.csv')
dt
Enter fullscreen mode Exit fullscreen mode

Relative path

dt1<- read.csv('./data_ziped/data_ziped.csv')
Enter fullscreen mode Exit fullscreen mode

Create folder and files

Create a folder inside the working directory:

dir.create('data')
dir.create('data/test')
Enter fullscreen mode Exit fullscreen mode

Create an empty file inside the directory:

file.create("test_text_file.txt")
file.create("test_word_file.docx")
file.create("test_csv_file.csv")
Enter fullscreen mode Exit fullscreen mode

Create 10 empty csv file inside directory using sapply function

sapply(paste0("test", 1:10, ".csv"), file.create)
Enter fullscreen mode Exit fullscreen mode

Delete file

unlink('test_word_file.docx')
file.remove('test_text_file.txt')
Enter fullscreen mode Exit fullscreen mode

Delete 5 files with sapply function

sapply(paste0('test', 1:5, '.csv'), unlink)
Enter fullscreen mode Exit fullscreen mode

Copy file

# file.copy("name_file.csv", "destination_folder")
file.copy("test5.csv", "D:/R4Researchers/data/test")
Enter fullscreen mode Exit fullscreen mode

List files in the directory

list.files("D:/R4Researchers/", full.names = TRUE, recursive = TRUE)
# list all CSV files non-recursively
list.files(pattern = ".csv")

# list all CSV files recursively through each sub-folder
list.files(pattern = ".csv", recursive = TRUE)
Enter fullscreen mode Exit fullscreen mode

Check if a file or directory exists

file.exists("D:/R4Researchers/data/test")
# TRUE
file.exists("D:/R4Researchers/data/test.csv")
# FALSE
file.exists('D:/R4Researchers/data')
# TRUE 
dir.exists('D:/R4Researchers')
# TRUE
dir.exists('D:/R4Researchers/new_data')
# FALSE
Enter fullscreen mode Exit fullscreen mode

If you like the content, please SUBSCRIBE to my channel for the future content

To get full video tutorial and certificate, please, enroll in the course through this link:
https://www.udemy.com/course/r-for-research/?referralCode=B6DCFDE343F0592EA61A

Top comments (0)