read_data: Read a data set from a file

View source: R/io.R

read_dataR Documentation

Read a data set from a file

Description

read_data() reads tabular data from many formats commonly used in data science (see supported_formats()) and returns a plain data.frame. The format is inferred from the file extension unless format is given.

If path is a directory (for example as written by generate_to_file()), all files in it with a recognised extension are read and row-bound.

Because text formats (CSV, JSON, Excel) do not store column types, a template data frame can be supplied: columns are then coerced to the classes of the template (factor levels, Date, POSIXct, integer, ...) with match_types().

Usage

read_data(
  path,
  format = NULL,
  template = NULL,
  object = NULL,
  dataset = "data",
  sheet = 1L,
  ...
)

Arguments

path

File or directory path.

format

Optional format name or extension, e.g. "parquet".

template

Optional data frame whose column classes are imposed on the result.

object

For rdata files: name of the object to load (default: the first data frame in the file).

dataset

For hdf5 files: name of the group (written by write_data()) or of a 1- or 2-dimensional dataset to read. Default "data".

sheet

For Excel files: sheet name or number.

...

Further arguments passed to the backend reader.

Value

A data.frame.

See Also

write_data(), supported_formats(), match_types()

Examples

f <- tempfile(fileext = ".csv")
write_data(iris, f)
x <- read_data(f, template = iris)
str(x)

f2 <- tempfile(fileext = ".rds")
write_data(iris, f2)
identical(read_data(f2), iris)

SynthesizerPlus documentation built on Oct. 16, 2026, 5:10 p.m.