** readLines()** function in R Language reads text lines from an input file. The

`readLines()`

function is perfect for text files since it reads the text line by line and creates character objects for each of the lines.

Syntax:readLines(path)

Parameter:

path:path of the file

**Example 1:**

`# R program to illustrate ` `# readLines() function ` ` ` `# Store currently used directory ` `path <` `-` `getwd() ` ` ` `# Write example text to currently used directory ` `write.table(x ` `=` `"the first line\nthe second line\nthe third line"` `, ` ` ` `file` `=` `paste(path, ` `"/my_txt.txt"` `, sep ` `=` `""), ` ` ` `row.names ` `=` `FALSE, col.names ` `=` `FALSE, quote ` `=` `FALSE) ` ` ` `# Apply readLines function to txt file ` `my_txt <` `-` `readLines(paste(path, ` `"/my_txt.txt"` `, sep ` `=` `"")) ` `my_txt ` |

*chevron_right*

*filter_none*

**Output:**

[1] "the first line" "the second line" "the third line"

**Example 2:**

`# R program to illustrate ` `# readLines() function ` ` ` `# Store currently used directory ` `path <` `-` `getwd() ` ` ` `# Write example text to currently used directory ` `write.table(x ` `=` `"the first line\nthe second line\nthe third line"` `, ` ` ` `file` `=` `paste(path, ` `"/my_txt.txt"` `, sep ` `=` `""), ` ` ` `row.names ` `=` `FALSE, col.names ` `=` `FALSE, quote ` `=` `FALSE) ` ` ` `# Apply readLines function to first two lines ` `my_txt_ex2 <` `-` `readLines(paste(path, ` `"/my_txt.txt"` `, sep ` `=` `""), ` ` ` `n ` `=` `2` `) ` `my_txt_ex2 ` |

*chevron_right*

*filter_none*

**Output:**

[1] "the first line" "the second line"

## Recommended Posts:

- Read contents of a CSV File in R Programming - read.csv() Function
- Addition of Lines to a Plot in R Programming - lines() Function
- Reading contents of a Text File in R Programming - read.table() Function
- Scan and Read Data from a File in R Programming - scan() Function
- Adding Straight Lines to a Plot in R Programming - abline() Function
- Printing out to the Screen or to a File in R Programming - cat() Function
- How To Import Data from a File in R Programming
- File Handling in R Programming
- Check if a Function is a Primitive Function in R Programming - is.primitive() Function
- Compute Density of the Distribution Function in R Programming - dunif() Function
- Apply a Function over a List of elements in R Programming - lapply() Function
- Search the Interval for Minimum and Maximum of the Function in R Programming - optimize() Function
- Compute the Natural Logarithm of the Absolute Value of Gamma Function in R Programming - lgamma() Function
- Compute the Logarithmic Derivative of the gamma Function in R Programming - digamma() Function
- Compute the Second Derivative of the Logarithmic value of the gamma Function in R Programming - trigamma() Function
- Get the List of Arguments of a Function in R Programming - args() Function
- Recursively apply a Function to a List in R Programming - rapply() function
- Apply a Function over a Ragged Array in R Programming - tapply() Function
- Applying a Function over an Object in R Programming - sapply() Function
- Compute the value of Cauchy Quantile Function in R Programming - qcauchy() Function

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.