R Language Reading and writing strings Reading from or writing to a file connection


Example

Not always we have liberty to read from or write to a local system path. For example if R code streaming map-reduce must need to read and write to file connection. There can be other scenarios as well where one is going beyond local system and with advent of cloud and big data, this is becoming increasingly common. One of the way to do this is in logical sequence.

Establish a file connection to read with file() command ("r" is for read mode):

conn <- file("/path/example.data", "r") #when file is in local system
conn1 <- file("stdin", "r") #when just standard input/output for files are available

As this will establish just file connection, one can read the data from these file connections as follows:

line <- readLines(conn, n=1, warn=FALSE)

Here we are reading the data from file connection conn line by line as n=1. one can change value of n (say 10, 20 etc.) for reading data blocks for faster reading (10 or 20 lines block read in one go). To read complete file in one go set n=-1.

After data processing or say model execution; one can write the results back to file connection using many different commands like writeLines(),cat() etc. which are capable of writing to a file connection. However all of these commands will leverage file connection established for writing. This could be done using file() command as:

conn2 <- file("/path/result.data", "w") #when file is in local system
conn3 <- file("stdout", "w") #when just standard input/output for files are available

Then write the data as follows:

writeLines("text",conn2, sep = "\n")