How does file handling work in R programming?

In the R programming language, we deal with large amounts of data by representing data in files. In this regard, we can perform certain operations to access data like creating files, reading files, renaming them, etc.

Some different file operations in R are:

  1. Creation of files
  2. Writing to the files
  3. Reading data from a file
  4. Check the existing status of a file
  5. Renaming the existing files

1. Creation of a file

It is the first operation that is performed in file handling. R enables us to escape the mutual creation of files, such as it permits us to create runtimes files in a specific location.

Syntax

file.create("file-name-with-extension")

Demo Code

In this example, we created a text file with the name of EdPresso.txt with the file.create() function. Upon successful execution, this method will return the Boolean value true. Otherwise, it will return false. In the code below, it will print Congrats! Your File Has been created. upon successful creation of the file, otherwise it will print Alas! Unable to Create File.

#creating file
if (file.create("EdPresso.txt")) {
print('Congrats! Your File Has been created.')
} else {
print('Alas! Unable to Create File')
}
# returns boolean value

2. Writing to the files

Writing to the files is one of the critical data manipulation operations of files in R. It provides a write.table() function that permits writing the data with respect to a tabular format.

Syntax

write.table(x= data, file =  "file-name-with-extension")

Parameters

  • x: represents the data that we want to write
  • file: Indicates the file that has to be written

Demo Code

We added ten rows from the ToothGrowth dataset built-in to the file EdPresso.txt using the write.table() function. Form line three and four by reading and printing data on the console using the print() method.

# calling wirte method
write.table(x = ToothGrowth[1:10, ], "EdPresso.txt")
data = read.table("EdPresso.txt")
print(data) # priting data

3. Reading data from a file

After the writing data onto a file, we need to read the information from the file using a built-in function. We use the read.table() function to read the file’s content that is passed as an argument.

Syntax

read.table("file-name-to-read-with-extension")

Parameters

  • file: this method takes the file name with extension as parameter.

Demo Code

In line three, we are using the read.table() method to read data from EdPresso.txt which contains the first ten rows of the ToothGrowth dataset. Check the code above and print on the console through the print() method.

EdPresso.txt

len

supp

dose

4.2

 VC

0.5

11.5

 VC

0.5

7.3

 VC

0.5

5.8

 VC

0.5

6.4

 VC

0.5

10.0

 VC

0.5

11.2 

 VC

0.5

11.2

 VC

0.5

5.2

 VC

0.5

7.0

 VC

0.5

write.table(x = ToothGrowth[1:10, ], "EdPresso.txt")
# reading data from EdPresso.txt
data = read.table("EdPresso.txt")
print(data) # priting data

4. Check an existing file

We can check the file if it exists or not within the current directory or on the mentioned path using the file.exists() function. We need to pass the file name, and if the file name is in existence, it returns TRUE. Otherwise, it returns FALSE.

Syntax

file.exist("file-name")

Demo Code

In line five, file.exists() returns TRUE because EdPresso.txt exists in the current directory. On the other hand, in line eleven, it returns FALSE. Why? Educative.txt does not exist. The content in the EdPresso.txt file is given below.

EdPresso.txt

Concise shots of dev knowledge

main.r
EdPresso.txt
# Checking existance of file
# Exist in current dir
if (file.exists("EdPresso.txt")) {
print('Your File `EdPresso.txt` Exist!')
} else {
print('Alas! File `EdPresso.txt` is Unavailable')
}
# Does not exist
if (file.exists("Educative.txt")) {
print('Your File `Educative.txt` Exist!')
} else {
print('Alas! File `Educative.txt` is Unavailable')
}

5. Rename files

Instead of creating a file, R also facilitates you to rename files using the file.rename() function. It neither creates a new file nor changes the file’s content. It only alters the existing file’s name to a new name and returns TRUE after successful completion. If the file to rename is not found, it terminates with the FALSE value.

Syntax

file.rename("old-name", "new-name")

Parameters

  • old-name: Current file name which you want to update
  • new-name: New file name which you want.

Demo Code

The code snippet above shows how to use the file.rename() method in R. Upon success, it prints Successfully updated otherwise, it shows No such file exists. Our EdPresso.txt is below:

EdPresso.txt

Concise shots of dev knowledge

main.r
EdPresso.txt
# method to change current file name
# Return False
if (file.rename("EdPresso.txt", "Educative.txt") ) {
print("Successfully updated")
} else{
print("No such file exists")
}

Free Resources