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.
read_lines(
file,
skip = 0,
skip_empty_rows = FALSE,
n_max = Inf,
locale = default_locale(),
na = character(),
lazy = should_read_lazy(),
num_threads = readr_threads(),
progress = show_progress()
)read_lines_raw(
file,
skip = 0,
n_max = -1L,
num_threads = readr_threads(),
progress = show_progress()
)
write_lines(
x,
file,
sep = "\n",
na = "NA",
append = FALSE,
num_threads = readr_threads(),
path = deprecated()
)
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.
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. To be recognised as
literal data, the input must be either wrapped with I()
, be a string
containing at least one new line, or be a vector containing at least one
string with a new line.
Using a value of clipboard()
will read from the system clipboard.
Number of lines to skip before reading data.
Should blank rows be ignored altogether? i.e. If this
option is TRUE
then blank rows will not be represented at all. If it is
FALSE
then they will be represented by NA
values in all the columns.
Number of lines to read. If n_max
is -1, all lines in
file will be read.
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.
Character vector of strings to interpret as missing values. Set this
option to character()
to indicate no missing values.
Read values lazily? By default, this is FALSE
, because there
are special considerations when reading a file lazily that have tripped up
some users. Specifically, things get tricky when reading and then writing
back into the same file. But, in general, lazy reading (lazy = TRUE
) has
many benefits, especially for interactive use and when your downstream work
only involves a subset of the rows or columns.
Learn more in should_read_lazy()
and in the documentation for the
altrep
argument of vroom::vroom()
.
The number of processing threads to use for initial
parsing and lazy reading of data. If your data contains newlines within
fields the parser should automatically detect this and fall back to using
one thread only. However if you know your file has newlines within quoted
fields it is safest to set num_threads = 1
explicitly.
Display a progress bar? By default it will only display
in an interactive session and not while knitting a document. The automatic
progress bar can be disabled by setting option readr.show_progress
to
FALSE
.
A character vector or list of raw vectors to write to disk.
The line separator. Defaults to \\n
, commonly used on POSIX
systems like macOS and linux. For native windows (CRLF) separators use
\\r\\n
.
If FALSE
, will overwrite existing file. If TRUE
,
will append to existing file. In both cases, if the file does not exist a new
file is created.
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, lazy = FALSE)
read_file(tmp) # note trailing \n
write_lines(airquality$Ozone, tmp, na = "-1")
read_lines(tmp)
Run the code above in your browser using DataLab