How to Use readLines() Function in R

Spread the love

In this extensive article, we will explore the readLines() function, a versatile R function used to read text data, line by line, from files or connections.

Overview of readLines() Function

The readLines() function is part of R’s base package, meaning you don’t need to install any additional packages to use it. It provides an effective way to read text files into R, where each line of the file forms an element of a character vector.

This function is not just limited to reading local files; it can also read data directly from a URL or any valid connection, making it a versatile tool in your R programming toolkit.

Here’s the syntax of the readLines() function:

readLines(con, n = -1L, ok = TRUE, warn = TRUE, encoding = "UTF-8", skipNul = FALSE)
  • con: a connection object or a character string naming a file or a URL.
  • n: number of lines to read. The default, -1L, means read all lines.
  • ok: a logical value. If FALSE, an error occurs if the connection cannot be opened.
  • warn: logical value. If TRUE, warnings are printed if the connection cannot be opened.
  • encoding: character string encoding to be used. The default is "UTF-8".
  • skipNul: logical value. If TRUE, nul characters are skipped.

Reading a Local Text File with readLines()

Let’s start by reading a local text file using readLines(). Assuming you have a text file named “file.txt” located in your current working directory:

# Read the text file
data <- readLines("file.txt")

# Print the data

In this example, readLines() reads the file and returns a character vector, where each element corresponds to a line in the file.

Reading a Text File from a URL with readLines()

As mentioned before, readLines() is not limited to reading local files. You can use it to read a text file directly from a URL. Here’s how you do it:

# Specify the URL of the text file
url <- ""

# Read the text file from the URL
data <- readLines(url)

# Print the data

Just replace "" with your actual URL. The function will download and read the text file, forming a character vector of its contents.

Working with Large Files

When working with large text files, you might not want to read the entire file at once. The n parameter in the readLines() function lets you specify how many lines to read:

# Read the first 100 lines of the text file
data <- readLines("large_file.txt", n = 100)

In this example, readLines() only reads the first 100 lines of the file. You can adjust this number based on your requirements and system memory limitations.

Error Handling and Encoding

Sometimes, you may encounter issues while reading a file, such as the file not existing or not having the right permissions. The ok and warn parameters of the readLines() function help manage these errors. If ok is set to FALSE, the function will stop and throw an error if the file cannot be opened. If warn is set to TRUE, the function will print a warning message when it can’t open the file.

The encoding parameter allows you to specify the character encoding of the file. This is crucial when dealing with international text to ensure the characters are correctly represented. The default encoding is "UTF-8", which covers a wide range of characters and is suitable for most cases.

Closing Connections

When you open a connection using the url(), file(), or gzfile() functions, you should close the connection after using it with the close() function:

# Open a connection to a text file
con <- file("data.txt")

# Read the file
data <- readLines(con)

# Close the connection

This is good practice and prevents potential problems with too many connections being left open.


The readLines() function is a powerful tool for reading text data into R from files and connections. It offers flexibility in reading a specific number of lines, handling errors, and specifying character encoding. This function, along with other data reading functions in R, provides you with the flexibility and control you need when importing data for your data analysis or data science tasks.

Posted in RTagged

Leave a Reply