Skip to contents

Reads a worksheet from an Excel workbook and returns an Arrow Table by default.

Usage

read_excel(
  file,
  sheet = 1,
  range = NULL,
  columns = NULL,
  col_names = TRUE,
  header_row = NULL,
  skip_rows = NULL,
  n_max = Inf,
  schema_sample_rows = NULL,
  dtype_coercion = c("coerce", "strict"),
  dtypes = NULL,
  skip_whitespace_tail_rows = FALSE,
  whitespace_as_null = FALSE,
  as = c("arrow_table", "arrow_record_batch", "arrow_array", "tibble", "data.frame",
    "vector")
)

Arguments

file

Path to an Excel workbook, or a raw vector containing workbook bytes.

sheet

Worksheet to read, either as a 1-based sheet index or a sheet name.

range

Optional Excel-style column range, such as "A:A" for one column or "A:D" for multiple columns.

columns

Optional columns to select, as a character vector of column names or a numeric vector of 1-based column positions. Cannot be combined with range.

col_names

TRUE to use the first row as names, FALSE to generate names, or a character vector of explicit names.

header_row

1-based row containing column names when col_names = TRUE. Use NULL to let the reader use its default first non-empty row behavior.

skip_rows

Number of rows to skip after the header row. Use NULL for the upstream default of skipping only empty leading rows.

n_max

Maximum number of data rows to read.

schema_sample_rows

Number of rows to sample for schema inference. Use NULL to sample all rows.

dtype_coercion

How to handle cells that do not match the inferred or specified type: "coerce" or "strict".

dtypes

Optional type override. Supply a single dtype string to apply to all selected columns, or a named character vector mapping column names to dtypes. Supported values are "null", "int", "float", "string", "boolean", "datetime", "date", and "duration".

skip_whitespace_tail_rows

Whether to ignore trailing rows containing only whitespace and null values.

whitespace_as_null

Whether string cells containing only whitespace are treated as missing values.

as

Type of object to return: "arrow_table", "arrow_record_batch", "arrow_array", "tibble", "data.frame", or "vector".

Value

For as = "arrow_table", an arrow::Table. For as = "arrow_record_batch", an arrow::RecordBatch. For as = "arrow_array", an arrow::Array, valid only when exactly one column is selected. For as = "tibble", a tibble. For as = "data.frame", a base data frame. For as = "vector", a named list of base R vectors for multi-column output, or a bare base R vector when exactly one column is selected. as = "arrow_array" differs from as = "vector": it returns an Arrow array, not an R vector.

Error classes

All package errors inherit from fastexcel_error. More specific subclasses are used where possible: fastexcel_validation_error, fastexcel_resource_limit_error, fastexcel_parse_error, and fastexcel_dependency_error.

Examples

file <- system.file("extdata/Pop_Density.xlsx", package = "fastexcel")
if (nzchar(file)) {
  read_excel(file)
  read_excel(file, as = "arrow_record_batch")
  read_excel(file, range = "A:A", as = "arrow_array")
  read_excel(file, as = "data.frame")
  read_excel(file, range = "A:A", as = "vector")
}
#> [1] "Austin"          "Oklahoma City"   "Montebello"      "Los Angeles"    
#> [5] "Kansas City, KS" "Kansas City, MO"