Reading Files in R Programming Language

Reading and writing files are basic tasks needed for many R programs. First we’ll look at some examples of reading files.

# Reading files requires checking most calls for errors.
# This helper will streamline our error checks below.
check <- function(e) {
  if (!is.null(e)) {
    stop(e)
  }
}

# Perhaps the most basic file reading task is
# slurping a file's entire contents into memory.
dat <- tryCatch(
  readChar("/tmp/dat", file.info("/tmp/dat")$size),
  error = function(e) e
)
check(dat)
cat(dat)

# You'll often want more control over how and what
# parts of a file are read. For these tasks, start
# by opening a file connection.
f <- tryCatch(
  file("/tmp/dat", "rb"),
  error = function(e) e
)
check(f)

# Read some bytes from the beginning of the file.
# Allow up to 5 to be read but also note how many
# actually were read.
b1 <- raw(5)
n1 <- tryCatch(
  readBin(f, what = raw(), n = 5),
  error = function(e) e
)
check(n1)
cat(sprintf("%d bytes: %s\n", length(n1), rawToChar(n1)))

# You can also seek to a known location in the file
# and read from there.
seek(f, 6)
b2 <- raw(2)
n2 <- tryCatch(
  readBin(f, what = raw(), n = 2),
  error = function(e) e
)
check(n2)
cat(sprintf("%d bytes @ %d: ", length(n2), 6))
cat(sprintf("%s\n", rawToChar(n2)))

# Other methods of seeking are relative to the
# current cursor position,
seek(f, 4, origin = "current")

# and relative to the end of the file.
seek(f, -10, origin = "end")

# The readBin function provides more control over reading.
seek(f, 6)
b3 <- raw(2)
n3 <- tryCatch(
  readBin(f, what = raw(), n = 2),
  error = function(e) e
)
check(n3)
cat(sprintf("%d bytes @ %d: %s\n", length(n3), 6, rawToChar(n3)))

# There is no built-in rewind, but
# seek(f, 0) accomplishes this.
seek(f, 0)

# The readLines function implements a buffered
# reader that may be useful both for its efficiency
# with many small reads and because of the additional
# reading methods it provides.
r4 <- readLines(f, n = 1)
cat(sprintf("5 bytes: %s\n", substr(r4, 1, 5)))

# Close the file when you're done (usually this would
# be scheduled immediately after opening with on.exit()).
close(f)

To run the program:

$ echo "hello" > /tmp/dat
$ echo "go" >>   /tmp/dat
$ Rscript reading-files.R
hello
go
5 bytes: hello
2 bytes @ 6: go
2 bytes @ 6: go
5 bytes: hello

Next we’ll look at writing files.