Home » Directories, Folders & Files

Recent Posts

Recent Comments

No comments to show.

Archives

Categories

Directories, Folders & Files

Understanding file paths in R

When working with directories in R, path formatting is crucial. While Windows uses backslashes (\) in file paths, R requires forward slashes (/). For example, the Windows path:

“C:\Users\stat\OneDrive\Desktop\Rmethods”

Should be written in R as:

“C:/Users/stat/OneDrive/Desktop/Rmethods”

Creating new directory

dir.create() function establishes new directories in the specified location. For instance, to create a subdirectory named ‘R’ within the ‘Rmethods’ folder:

dir.create(“C:/Users/stat/OneDrive/Desktop/Rmethods/R”)

Important considerations when creating directories:

  • Always enclose paths in double quotes
  • Use forward slashes (/) for path separation
  • Set showWarnings = TRUE to receive creation notifications
  • Enable recursive = TRUE to create parent directories if they don’t exist
  • Specify mode = 0777 to grant full read, write, and execute permissions

Try using the dir.create.unique function to create a uniquely named directory.

Checking directory existence

dir.exists() confirms if the directory exists & returns TRUE if successful else FALSE:.

dir.exists(“C:/Users/stat/OneDrive/Desktop/Rmethods/R”)

[1] TRUE

Getting contents in directory

dir() function gets list of contents in the current directory.

dir()

[1] “141024.R”    “car1.txt”    “cars.txt”    “mtcars.xlsx” “mtcars1.csv”

Getting previous working directory in the path

dirname() gives the previous directory in the path.

dirname(“C:/Users/stat/OneDrive/Desktop/Rmethods/R”)

[1] “C:/Users/stat/OneDrive/Desktop/Rmethods”

Getting last working directory

basename() provides the complete path of the working directory.

basename(“C:/Users/stat/OneDrive/Desktop/Rmethods/R”)

[1] “R”

Checking current directory

getwd() provides the current working directory.

getwd()

[1] “C:/Users/stat/OneDrive/Desktop/PixelR/R”

Setting working directory

setwd() to set ‘R’ as working directory under ‘Rmethods’.

setwd(“C:/Users/stat/OneDrive/Desktop/Rmethods/R”)

Removing directory

unlink() removes a directory & its content.

unlink(“C:/Users/stat/OneDrive/Desktop/Rmethods/R”, recursive = TRUE)

Checking if a file exists

file.exists() to check if any file exits in the directory & returns TRUE if successful else FALSE:

file.exists(“abc.txt”)

Removing files

file.remove() removes a particular file & returns TRUE if successful else FALSE:

file.remove(“mtcars1.csv”)


Remember, if working on current working directory, don’t have to give the path. Otherwise, required.

Renaming files

file.rename() changes a file name & returns TRUE if successful else FALSE:

file.rename(from = “car1.txt”, to = “car2.txt”)

Creating files

file.create() creates a new file & returns TRUE if successful else FALSE:

file.create(“homework.txt”)

Copying files

file.copy() duplicates files while preserving the original & returns TRUE if successful else FALSE:

file.copy(from = “car2.txt”, to = “car1.txt”)