R: Importing csv files into ff data.frames

read.table.ffdf {ff} | R Documentation

## Importing csv files into ff data.frames

### Description

Function `read.table.ffdf` reads separated flat files into `ffdf` objects, very much like (and using) `read.table`. It can also work with any convenience wrappers like `read.csv` and provides its own convenience wrapper (e.g. `read.csv.ffdf`) for R's usual wrappers.

### Usage

    read.table.ffdf(
      x = NULL
    , file, fileEncoding = ""
    , nrows = -1, first.rows = NULL, next.rows = NULL
    , levels = NULL, appendLevels = TRUE
    , FUN = "read.table", ...
    , transFUN = NULL
    , asffdf_args = list()
    , BATCHBYTES = getOption("ffbatchbytes")
    , VERBOSE = FALSE
    )
    read.csv.ffdf(...)
    read.csv2.ffdf(...)
    read.delim.ffdf(...)
    read.delim2.ffdf(...)

### Arguments

`x` | NULL or an optional `ffdf` object to which the read records are appended. If this is provided, it defines crucial features that are otherwise determnined during the 'first' chunk of reading: `vmodes`, `colnames`, `colClasses`, sequence of predefined `levels`. In order to also read the first chunk into such predefined ffdf, an `x` with 1 row is treated special: instead of appending the first row will be overwritten. This is necessary because we cannot provide `x` with zero rows (we cannot create ff vectors with zero elements).
`file` | the name of the file which the data are to be read from. Each row of the table appears as one line of the file. If it does not contain an absolute path, the file name is relative to the current working directory, `getwd()`. Tilde-expansion is performed where supported. Alternatively, `file` can be a readable text-mode `connection` (which will be opened for reading if necessary, and if so `close`d (and hence destroyed) at the end of the function call).
`fileEncoding` | character string: if non-empty declares the encoding used on a file (not a connection) so the character data can be re-encoded. See `file`.
`nrows` | integer: the maximum number of rows to read in (includes first.rows in case a 'first' chunk is read) Negative and other invalid values are ignored.
`first.rows` | integer: number of rows to be read in the first chunk, see details. Default is the value given at `next.rows` or `1e3` otherwise. Ignored if `x` is given.
`next.rows` | integer: number of rows to be read in further chunks, see details. By default calculated as `BATCHBYTES %/% sum(.rambytes[vmode(x)])`
`levels` | NULL or an optional list, each element named with col.names of factor columns specifies the `levels` Ignored if `x` is given.
`appendLevels` | logical. A vector of permissions to expand `levels` for factor columns. Recycled as necessary, or if the logical vector is named, unspecified values are taken to be `TRUE`. Ignored during processing of the 'first' chunk
`FUN` | character: name of a function that is called for reading each chunk, see `read.table`, `read.csv`, etc.
`...` | further arguments, passed to `FUN` in `read.table.ffdf`, or passed to `read.table.ffdf` in the convenience wrappers
`transFUN` | NULL or a function that is called on each data.frame chunk after reading with `FUN` and before further processing (for filtering, transformations etc.)
`asffdf_args` | further arguments passed to `as.ffdf` when converting the `data.frame` of the first chunk to `ffdf`. Ignored if `x` is given.
`BATCHBYTES` | integer: bytes allowed for the size of the `data.frame` storing the result of reading one chunk. Default `getOption("ffbatchbytes")`.
`VERBOSE` | logical: TRUE to verbose timings for each processed chunk (default FALSE)

### Details

`read.table.ffdf` has been designed to read very large (many rows) separated flatfiles in row-chunks and store the result in a `ffdf` object on disk, but quickly accessible via `ff` techniques.
The first chunk is read with a default of 1000 rows, for subsequent chunks the number of rows is calculated to not require more RAM than `getOption("ffbatchbytes")`. The following could be indications to change the parameter `first.rows`:

1. set `first.rows=-1` to read the complete file in one go (requires enough RAM)

2. set `first.rows` to a smaller number if the pre-allocation of RAM for the first chunk with parameter `nrows` in `read.table` is too large, i.e. with many columns on machine with little RAM.

3. set `first.rows` to a larger number if you expect better factor level ordering (factor levels are sorted in the first chunk, but not at subsequent chunks, however, factor level ordering can be fixed later, see below).

By default the `ffdf` object is created on the fly at the end of reading the 'first' chunk, see argument `first.rows`. The creation of the `ffdf` object is done via `as.ffdf` and can be finetuned by passing argument `asffdf_args`. Even more control is possible by passing in a `ffdf` object as argument `x` to which the read records are appended.
`read.table.ffdf` has been designed to behave as much like `read.table` as possible. Hoever, note the following differences:

1. Arguments 'colClasses' and 'col.names' are now enforced also during 'next.rows' chunks. For example giving `colClasses=NA` will force that no colClasses are derived from the `first.rows` respective from the `ffdf` object in parameter `x`.

2. colClass 'ordered' is allowed and will create an `ordered` factor

3. character vector are not supported, character data must be read as one of the following colClasses: 'Date', 'POSIXct', 'factor, 'ordered'. By default character columns are read as factors. Accordingly arguments 'as.is' and 'stringsAsFactors' are not allowed.

4. the sequence of `levels.ff` from chunked reading can depend on chunk size: by default new levels found on a chunk are appended to the levels found in previous chunks, no attempt is made to sort and recode the levels during chunked processing, levels can be sorted and recoded most efficiently after all records have been read using `sortLevels`.

5. the default for argument 'comment.char' is `""` even for those FUN that have a different default. However, explicit specification of 'comment.char' will have priority.

### Value

An `ffdf` object. If created during the 'first' chunk pass, it will have one `physical` component per `virtual` column.

### Note

Note that using the 'skip' argument still requires to read the file from beginning in order to count the lines to be skipped. If you first read part of the file in order to understand its structure and then want to continue, a more efficient solution that using 'skip' is opening a `file` `connection` and pass that to argument 'file'. `read.table.ffdf` does the same in order to skip efficiently over previously read chunks.

### Author(s)

Jens Oehlschlägel, Christophe Dutang

### See Also

`write.table.ffdf`, `read.table`, `ffdf`

### Examples

        x <- data.frame(log=rep(c(FALSE, TRUE), length.out=26), int=1:26, dbl=1:26 + 0.1
    , fac=factor(letters), ord=ordered(LETTERS)
    , dct=Sys.time()+1:26, dat=seq(as.Date("1910/1/1"), length.out=26, by=1))
        x <- x[c(13:1, 13:1),]
        csvfile <- tempPathFile(path=getOption("fftempdir"), extension="csv")
        write.csv(x, file=csvfile, row.names=FALSE)

        y <- read.csv(file=csvfile, header=TRUE)
        y
        cat("Read csv with header\n")
        ffx <- read.csv.ffdf(file=csvfile, header=TRUE)
        ffx
        sapply(ffx[,], class)

        message("NOTE that read.table fails for ordered factors, this is fixed in read.table.ffdf")
        try(read.csv(file=csvfile, header=TRUE, colClasses=c(ord="ordered")))
        # TODO could fix this with the following two commands (Gabor Grothendieck)
        # but does not know what bad side-effects this could have
         #setOldClass("ordered")
         #setAs("character", "ordered", function(from) ordered(from))
        y <- read.csv(file=csvfile, header=TRUE, colClasses=c(dct="POSIXct", dat="Date"))
        y

        ffx <- read.csv.ffdf(file=csvfile, header=TRUE
    , colClasses=c(ord="ordered", dct="POSIXct", dat="Date"))
        ffx
        sapply(ffx[,], class)

        message("NOTE that reading in chunks can change the sequence of levels and thus the coding")
        message("(Sorting levels during chunked reading can be too expensive)")
        ffx <- read.csv.ffdf(file=csvfile, header=TRUE
    , colClasses=c(ord="ordered", dct="POSIXct", dat="Date"), first.rows=6, next.rows=10
    , VERBOSE=TRUE)
        y <- ffx$fac[]
        print(levels(y))
        data.frame(values=as.character(y), codes=as.integer(y))

        message("If we don't know the levels we can sort then after reading")
        message("(Will rewrite all factor codes)")
        message("NOTE that you MUST assign the return value of sortLevels()")
        ffx <- sortLevels(ffx)
        y <- ffx$fac[]
        print(levels(y))
        data.frame(values=as.character(y), codes=as.integer(y))

        message("If we KNOW the levels we can fix levels upfront")
        ffx <- read.csv.ffdf(file=csvfile, header=TRUE
    , colClasses=c(ord="ordered", dct="POSIXct", dat="Date")
    , first.rows=6, next.rows=10, levels=list(fac=letters, ord=LETTERS))
        y <- ffx$fac[]
        print(levels(y))
        data.frame(values=as.character(y), codes=as.integer(y))

        message("Or we inspect a sufficiently large chunk of data and use those")
        ffx1 <- read.csv.ffdf(file=csvfile, header=TRUE
    , colClasses=c(ord="ordered", dct="POSIXct", dat="Date"), nrows=13)
        ffx <- read.csv.ffdf(x=ffx1, file=csvfile, header=FALSE, skip=1+nrow(ffx1), VERBOSE=TRUE)

        message("We can check for unexpected factor levels, say we only allowed a:l")
        ffx <- read.csv.ffdf(file=csvfile, header=TRUE
    , colClasses=c(ord="ordered", dct="POSIXct", dat="Date")
    , levels=list(fac=letters[1:12], ord=LETTERS[1:12]), appendLevels=FALSE)
        sapply(colnames(ffx), function(i)sum(is.na(ffx[[i]][])))

        message("We can fine-tune the creation of the ffdf:")
        message("- let's create the ff files outside of fftempdir")
        message("- let's reduce required disk space and thus file.system cache RAM")
        vmode(ffx)
        message("By default we had record size")
        sum(.ffbytes[vmode(ffx)])

        ffy <- read.csv.ffdf(file=csvfile, header=TRUE
        , colClasses=c(ord="ordered", dct="POSIXct", dat="Date")
        , asffdf_args=list(
            vmode = c(log="boolean", int="byte", dbl="single", fac="nibble"
                    , ord="nibble", dct="single", dat="single")
          , col_args=list(pattern = "./csv")  # create in getwd() with prefix csv
          )
        )
        vmode(ffy)
        message("This recordsize is more than 50% reduced")
        sum(.ffbytes[vmode(ffy)])

        message("Don't forget to wrap-up files that are not in fftempdir")
        delete(ffy); rm(ffy)
        message("It's a good habit to also wrap-up temporary stuff (or at least know how this is done)")
        rm(ffx); gc()

        fwffile <- tempfile()

        cat(file=fwffile, "123456", "987654", sep="\n")
        x <- read.fwf(fwffile, widths=c(1,2,3))    #> 1 23 456 \ 9 87 654
        y <- read.table.ffdf(file=fwffile, FUN="read.fwf", widths=c(1,2,3))
        stopifnot(identical(x, y[,]))
        x <- read.fwf(fwffile, widths=c(1,-2,3))   #> 1 456 \ 9 654
        y <- read.table.ffdf(file=fwffile, FUN="read.fwf", widths=c(1,-2,3))
        stopifnot(identical(x, y[,]))
        unlink(fwffile)

        cat(file=fwffile, "123", "987654", sep="\n")
        x <- read.fwf(fwffile, widths=c(1,0, 2,3))    #> 1 NA 23 NA \ 9 NA 87 654
        y <- read.table.ffdf(file=fwffile, FUN="read.fwf", widths=c(1,0, 2,3))
        stopifnot(identical(x, y[,]))
        unlink(fwffile)

        cat(file=fwffile, "123456", "987654", sep="\n")
        x <- read.fwf(fwffile, widths=list(c(1,0, 2,3), c(2,2,2))) #> 1 NA 23 456 98 76 54
        y <- read.table.ffdf(file=fwffile, FUN="read.fwf", widths=list(c(1,0, 2,3), c(2,2,2)))
        stopifnot(identical(x, y[,]))
        unlink(fwffile)

        unlink(csvfile)

[Package ff version 2.2-13 Index]