Reading a text file in R is possible and a user wants to read a text file in R and that too line by line.How to do that?

734    Asked by Uditisingh in Data Science , Asked on Nov 3, 2019
Answered by Uditi singh

Reading all the lines at a time is possible but can be ricky so to process the line one at a time for reading, we can do the following

processFile = function(filepath) {

connection = file(filepath, "r")

while ( TRUE ) {

lines = readLines(connection, n = 1)

 if ( length(lines) == 0 ) {

break

}

print(lines)

}

close(connection)

}



Your Answer

Interviews

Parent Categories