Saturday 27 June 2015

R : readLines : Read text lines from a file

By using readLines() method, you can read some/all text lines from a file/connection.

Syntax
readLines(con = stdin(), n = -1L, ok = TRUE, warn = TRUE,
          encoding = "unknown", skipNul = FALSE)

Parmeter
Description
con
Represens connection object to a file
n
Maximum number of lines to read. If n is negative, it should read up to the end of input on the connection.
ok
It is Boolean variable. Is it OK to reach the end of the connection before n > 0 lines are read? If not, an error will be generated.
warn
It is a Boolean variable. Warn if a text file is missing a final EOL or if there are embedded nuls in the file.
encoding
Encoding to be assumed for input strings.
skipNul
If it is TRUE, then null values are skipped, else not.

Let’s say “output.txt” contain following data.
first line
second line

third line

> fileConn <- file("output.txt")
> data <- readLines(fileConn)
> close(fileConn)
> 
> data
[1] "first line"  "second line" "third line" 



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment