R: Read/write lines to/from a file

read_lines {readr} | R Documentation

## Read/write lines to/from a file

### Description

`read_lines()` reads up to `n_max` lines from a file. New lines are not included in the output. `read_lines_raw()` produces a list of raw vectors, and is useful for handling data with unknown encoding. `write_lines()` takes a character vector or list of raw vectors, appending a new line after each entry.

### Usage

    read_lines(file, skip = 0, n_max = -1L, locale = default_locale(),
      na = character(), progress = show_progress())

    read_lines_raw(file, skip = 0, n_max = -1L, progress = show_progress())

    write_lines(x, path, na = "NA", append = FALSE)

### Arguments

`file` | Either a path to a file, a connection, or literal data (either a single string or a raw vector). Files ending in `.gz`, `.bz2`, `.xz`, or `.zip` will be automatically uncompressed. Files starting with `http://`, `https://`, `ftp://`, or `ftps://` will be automatically downloaded. Remote gz files can also be automatically downloaded and decompressed. Literal data is most useful for examples and tests. It must contain at least one new line to be recognised as data (instead of a path).
`skip` | Number of lines to skip before reading data.
`n_max` | Number of lines to read. If `n_max` is -1, all lines in file will be read.
`locale` | The locale controls defaults that vary from place to place. The default locale is US-centric (like R), but you can use `locale()` to create your own locale that controls things like the default time zone, encoding, decimal mark, big mark, and day/month names.
`na` | Character vector of strings to use for missing values. Set this option to `character()` to indicate no missing values.
`progress` | Display a progress bar? By default it will only display in an interactive session and not while knitting a document. The display is updated every 50,000 values and will only display if estimated reading time is 5 seconds or more. The automatic progress bar can be disabled by setting option `readr.show_progress` to `FALSE`.
`x` | A data frame to write to disk
`path` | Path or connection to write to.
`append` | If `FALSE`, will overwrite existing file. If `TRUE`, will append to existing file. In both cases, if file does not exist a new file is created.

### Value

`read_lines()`: A character vector with one element for each line. `read_lines_raw()`: A list containing a raw vector for each line.

`write_lines()` returns `x`, invisibly.

### Examples

    read_lines(file.path(R.home("doc"), "AUTHORS"), n_max = 10)
    read_lines_raw(file.path(R.home("doc"), "AUTHORS"), n_max = 10)

    tmp <- tempfile()

    write_lines(rownames(mtcars), tmp)
    read_lines(tmp)
    read_file(tmp) # note trailing \n

    write_lines(airquality$Ozone, tmp, na = "-1")
    read_lines(tmp)

[Package readr version 1.1.0 Index]