Skip to contents

fastexcel reads Excel workbooks through a Rust backend and returns Arrow objects by default. This keeps the read path efficient while still allowing you to convert to common R data structures when needed.

library(fastexcel)

file <- system.file("extdata/Pop_Density.xlsx", package = "fastexcel")

Read a worksheet

Call read_excel() with a workbook path. The default return value is an arrow::Table.

tbl <- read_excel(file)
tbl
#> Table
#> 6 rows x 5 columns
#> $city <string>
#> $Year <double>
#> $pop_density_mi <double>
#> $pop_density_km <double>
#> $density_ratio_to_KC <double>

You can select a sheet by 1-based index or by sheet name.

read_excel(file, sheet = 1, n_max = 2)
#> Table
#> 2 rows x 5 columns
#> $city <string>
#> $Year <double>
#> $pop_density_mi <double>
#> $pop_density_km <double>
#> $density_ratio_to_KC <double>
read_excel(file, sheet = "Sheet1", n_max = 2)
#> Table
#> 2 rows x 5 columns
#> $city <string>
#> $Year <double>
#> $pop_density_mi <double>
#> $pop_density_km <double>
#> $density_ratio_to_KC <double>

Choose a return type

Use as when you want a specific R object type.

read_excel(file, as = "data.frame")
#>              city Year pop_density_mi pop_density_km density_ratio_to_KC
#> 1          Austin 2020         3006.4      1156.3077           1.8621245
#> 2   Oklahoma City 2020         1123.5       432.1154           0.6958811
#> 3      Montebello 2020         7521.6      2892.9231           4.6587798
#> 4     Los Angeles 2020         8304.2      3193.9231           5.1435119
#> 5 Kansas City, KS 2020         1255.5       482.8846           0.7776401
#> 6 Kansas City, MO 2020         1614.5       620.9615           1.0000000
read_excel(file, as = "tibble")
#> # A tibble: 6 × 5
#>   city             Year pop_density_mi pop_density_km density_ratio_to_KC
#>   <chr>           <dbl>          <dbl>          <dbl>               <dbl>
#> 1 Austin           2020          3006.          1156.               1.86 
#> 2 Oklahoma City    2020          1124.           432.               0.696
#> 3 Montebello       2020          7522.          2893.               4.66 
#> 4 Los Angeles      2020          8304.          3194.               5.14 
#> 5 Kansas City, KS  2020          1256.           483.               0.778
#> 6 Kansas City, MO  2020          1614.           621.               1
read_excel(file, as = "arrow_record_batch")
#> RecordBatch
#> 6 rows x 5 columns
#> $city <string>
#> $Year <double>
#> $pop_density_mi <double>
#> $pop_density_km <double>
#> $density_ratio_to_KC <double>

For one-column reads, you can return either an Arrow array or a base R vector.

read_excel(file, range = "A:A", as = "arrow_array")
#> Array
#> <string>
#> [
#>   "Austin",
#>   "Oklahoma City",
#>   "Montebello",
#>   "Los Angeles",
#>   "Kansas City, KS",
#>   "Kansas City, MO"
#> ]
read_excel(file, range = "A:A", as = "vector")
#> [1] "Austin"          "Oklahoma City"   "Montebello"      "Los Angeles"    
#> [5] "Kansas City, KS" "Kansas City, MO"

Select rows and columns

Use Excel-style column ranges for contiguous column selections.

read_excel(file, range = "A:C", as = "data.frame")
#>              city Year pop_density_mi
#> 1          Austin 2020         3006.4
#> 2   Oklahoma City 2020         1123.5
#> 3      Montebello 2020         7521.6
#> 4     Los Angeles 2020         8304.2
#> 5 Kansas City, KS 2020         1255.5
#> 6 Kansas City, MO 2020         1614.5

Use columns to select by column name or 1-based column position.

read_excel(file, columns = c("city", "Year"), as = "data.frame")
#>              city Year
#> 1          Austin 2020
#> 2   Oklahoma City 2020
#> 3      Montebello 2020
#> 4     Los Angeles 2020
#> 5 Kansas City, KS 2020
#> 6 Kansas City, MO 2020
read_excel(file, columns = c(1, 3), as = "data.frame")
#>              city pop_density_mi
#> 1          Austin         3006.4
#> 2   Oklahoma City         1123.5
#> 3      Montebello         7521.6
#> 4     Los Angeles         8304.2
#> 5 Kansas City, KS         1255.5
#> 6 Kansas City, MO         1614.5

Use n_max to limit the number of returned data rows.

read_excel(file, n_max = 3, as = "data.frame")
#>            city Year pop_density_mi pop_density_km density_ratio_to_KC
#> 1        Austin 2020         3006.4      1156.3077           1.8621245
#> 2 Oklahoma City 2020         1123.5       432.1154           0.6958811
#> 3    Montebello 2020         7521.6      2892.9231           4.6587798

Read workbook bytes

file may also be a raw vector containing workbook bytes. This is useful when a cloud storage client returns the downloaded object directly in memory.

bytes <- readBin(file, what = "raw", n = file.info(file)$size)
bytes[1:5]
#> [1] 50 4b 03 04 14
read_excel(bytes, n_max = 2, as = "data.frame")
#>            city Year pop_density_mi pop_density_km density_ratio_to_KC
#> 1        Austin 2020         3006.4      1156.3077           1.8621245
#> 2 Oklahoma City 2020         1123.5       432.1154           0.6958811

For example, googleCloudStorageR::gcs_get_object() can return object bytes as a raw vector. Pass those bytes directly to read_excel() without writing a temporary workbook to disk.

object <- "path/to/workbook.xlsx"

bytes <- googleCloudStorageR::gcs_get_object(object)
read_excel(bytes, as = "data.frame")

Keeping the workbook in memory avoids creating extra local file artifacts. That is especially useful for sensitive data workloads where reducing disk writes can lower the chance of leaving behind temporary files, cached copies, or cleanup gaps outside the R process.