Read Lines from a File in R Programming - readLines() Function Last Updated : 01 Jul, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Python3 # 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 Output: [1] "the first line" "the second line" "the third line" Example 2: Python3 # 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 Output: [1] "the first line" "the second line" Comment More infoAdvertise with us Next Article Scan and Read Data from a File in R Programming - scan() Function A akhilsharma870 Follow Improve Article Tags : R Language R Functions R-FileHandling Similar Reads Scan and Read Data from a File in R Programming - scan() Function scan() function in R Language is used to scan and read data. It is usually used to read data into vector or list or from file in R Language. Syntax: scan("data.txt", what = "character") Parameter: data.txt: Text file to be scanned Returns: Scanned output Example 1: Scanning of Text r # R Program to 3 min read Reading contents of a Text File in R Programming - read.table() Function The read.table() function in R can be used to read a text file's contents. A versatile and often used function for reading tabular data from different file formats, including text files, is read.table(). It returns the data in the form of a table.Syntax: read.table(filename, header = FALSE, sep = "" 2 min read Read contents of a CSV File in R Programming - read.csv() Function read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. The read.csv() function also accepts a number of optional arguments that we can use to modify the import procedure. we can choose to treat the first row as column names, sele 3 min read Reading Tabular Data from files in R Programming Often, the data which is to be read and worked upon is already stored in a file but is present outside the R environment. Hence, importing data into R is a mandatory task in such circumstances. The formats which are supported by R are CSV, JSON, Excel, Text, XML, etc. The majority of times, the data 4 min read Adding Straight Lines to a Plot in R Programming - abline() Function abline() function in R Language is used to add one or more straight lines to a graph. The abline() function can be used to add vertical, horizontal or regression lines to plot. Syntax: abline(a=NULL, b=NULL, h=NULL, v=NULL, ...) Parameters: a, b: It specifies the intercept and the slope of the line 2 min read Printing out to the Screen or to a File in R Programming - cat() Function cat() function in R Language is used to print out to the screen or to a file. Syntax: cat(..., file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE)Parameters: ...: atomic vectors, names, NULL and objects with no output file: the file in which printing will be done sep: specified separ 2 min read Like