EZ

Eduzan

Learning Hub

Eduzan
Eduzan / R (programming language)

File Handling

Computer Science / R (programming language) tutorial chapter - Published 2025-12-13 - R (programming language)

In R Programming, handling of files such as reading and writing files can be done by using in-built functions present in R base package. In this article, let us discuss reading and writing of CSV files, creating a file, renaming a file, check the existence of the file, listing all files in the working directory, copying files and creating directories.

Creating a File

Using file.create() function, a new file can be created from console or truncates if already exists. The function returns a TRUE logical value if file is created otherwise, returns FALSE.

Syntax:

file.create(" ")

Parameters:

  • ” “: The name of the file to be created.

Example:

# Create a file named Sample.txt
file.create("Sample.txt")

Output:

[1] TRUE

Writing to a File

The write.table() function allows you to write objects such as data frames or matrices to a file. This function is part of the utils package.

Syntax:

write.table(x, file)

Parameters:

  • x: The object to be written to the file.
  • file: The name of the file to write.

Example:

# Write the first 5 rows of mtcars dataset to Sample.txt
write.table(x = mtcars[1:5, ], file = "Sample.txt")

Output:

The content will be written to “Sample.txt” and can be opened in any text editor.

Renaming a File

The file.rename() function renames a file. It returns TRUE if successful, and FALSE otherwise.

Syntax:

file.rename(from, to)

Parameters:

  • from: The current name or path of the file.
  • to: The new name or path for the file.

Example:

# Rename Sample.txt to UpdatedSample.txt
file.rename("Sample.txt", "UpdatedSample.txt")

Output:

[1] TRUE

Checking File Existence

To check if a file exists, use the file.exists() function. It returns TRUE if the file exists, and FALSE otherwise.

Syntax:

file.exists(" ")

Parameters:

  • ” “: The name of the file to check.

Example:

# Check if Sample.txt exists
file.exists("Sample.txt")

# Check if UpdatedSample.txt exists
file.exists("UpdatedSample.txt")

Output:

[1] FALSE
[1] TRUE

Reading a File

The read.table() function reads files and outputs them as data frames.

Syntax:

read.table(file)

Parameters:

  • file: The name of the file to be read.

Example:

# Read UpdatedSample.txt
read_data <- read.table(file = "UpdatedSample.txt")
print(read_data)

Output:

mpg cyl disp  hp drat
1 21.0   6  160 110  3.9
2 21.0   6  160 110  3.9
3 22.8   4  108  93  3.8
4 21.4   6  258 110  3.1
5 18.7   8  360 175  3.2

Listing All Files

The list.files() function lists all files in the specified path. If no path is provided, it lists files in the current working directory.

Syntax:

list.files(path)

Parameters:

  • path: The directory path.

Example:

# List files in the current directory
list.files()

Output:

[1] "UpdatedSample.txt" "data.csv" "R_Script.R" "output.txt"

Copying a File

The file.copy() function creates a copy of a file.

Syntax:

file.copy(from, to)

Parameters:

  • from: The file path to copy.
  • to: The destination path.

Example:

# Copy UpdatedSample.txt to a new location
file.copy("UpdatedSample.txt", "Backup/UpdatedSample.txt")

# List files in Backup directory
list.files("Backup")

Output:

[1] TRUE
[1] "UpdatedSample.txt"

Creating a Directory

The dir.create() function creates a directory in the specified path. If no path is provided, it creates the directory in the current working directory.

Syntax:

dir.create(path)

Parameters:

  • path: The directory path with the new directory name at the end.

Example:

# Create a directory named DataFiles
dir.create("DataFiles")

# List files in the current directory
list.files()

Output:

[1] "DataFiles" "UpdatedSample.txt" "output.txt"
End of lesson.