Skip to content

Instantly share code, notes, and snippets.

@crsh
Last active February 1, 2018 10:47
Show Gist options
  • Save crsh/357458c41fd3d554fb24 to your computer and use it in GitHub Desktop.
Save crsh/357458c41fd3d554fb24 to your computer and use it in GitHub Desktop.
Function to quickly rbind multiple local data-files
batch_read <- function(path, pattern, recursive = FALSE, read_fun, ...) {
data.files <- list.files(path, pattern = pattern, recursive = recursive)
data <- lapply(paste0(path, data.files), read_fun, ...)
data <- do.call("rbind", data)
data
}
@crsh
Copy link
Author

crsh commented Apr 1, 2016

Arguments

path Character. Path to folder containing the data. Should end on /.
pattern Character. Regular expression (file extension or other parts of the file name).
read_fun Function. Function used to read data files (e.g. read.csv).
... Other parameters passed to read_fun.

Example

data <- batch_read(
  url = "path/to/files/"
  , pattern = "\\.csv"
  , read_fun = read.csv
  , header = TRUE
)

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