Skip to content

Instantly share code, notes, and snippets.

@PietrH
Created February 26, 2026 15:22
Show Gist options
  • Select an option

  • Save PietrH/9b73f6bde644d4f39a93fdd7235f7159 to your computer and use it in GitHub Desktop.

Select an option

Save PietrH/9b73f6bde644d4f39a93fdd7235f7159 to your computer and use it in GitHub Desktop.
What is the fastest way to read the header of a delim?
readr_lazy_colnames <- function(path, delim = "|") {
readr::read_delim(
path,
delim = delim,
lazy = TRUE,
progress = FALSE,
show_col_types = FALSE
) |>
colnames()
}
arrow_colnames <- function(path, delim = "|") {
arrow::open_delim_dataset(path, delim = delim) |>
colnames()
}
arrow_schema_colnames <- function(path, delim = "|") {
arrow::open_delim_dataset(path, delim = delim)$schema |>
names()
}
arrow_nodf_colnames <- function(path, delim = "|") {
arrow::read_delim_arrow(path, delim = delim, as_data_frame = FALSE) |>
colnames()
}
readr_nmax_colnames <- function(path, delim = "|") {
readr::read_delim(
path,
delim = delim,
n_max = 0,
progress = FALSE,
show_col_types = FALSE
) |>
colnames()
}
readlines_colnames <- function(path, delim = "|") {
strsplit(readLines(path, n = 1), split = delim, fixed = TRUE)[[1]]
}
vroom_colnames <- function(path, delim = "|") {
vroom::vroom(
path,
delim = delim,
n_max = 0 ,
show_col_types = FALSE,
progress = FALSE
) |>
colnames()
}
dt_colnames <- function(path, delim = "|") {
data.table::fread(
nrow = 0,
sep = delim,
header = TRUE,
cmd = sprintf("head -n 1 %s", path)
) |>
colnames()
}
# Big csv file (pipe separated in my case, not provided!
big_csv <- "~/Downloads/wcvp_taxon.csv"
bench::mark(
readr_lazy_colnames(big_csv),
readr_nmax_colnames(big_csv),
arrow_colnames(big_csv),
arrow_schema_colnames(big_csv),
arrow_nodf_colnames(big_csv),
readlines_colnames(big_csv),
vroom_colnames(big_csv),
dt_colnames(big_csv)
)
@PietrH
Copy link
Author

PietrH commented Feb 26, 2026

Using readlines is by far the fastest way to do this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment