Nothing
#' Import data
#'
#' Utility to import multiple data sets from a directory into a named list.
#'
#' @param location Character. Directory from which to import data.
#' @param extension Character. File extension to match (without ".").
#' @param import_function Function used to read each file (e.g., \code{readr::read_csv}).
#' @param recursive Logical. Search subdirectories? Default is \code{TRUE}.
#'
#' @return A named list of imported objects. Names are derived from filenames
#' without their extensions, and each element contains the result returned by
#' \code{import_function()} for one matching file.
#'
#' @author Saannidhya Rawat
#' @family utilities
#' @export
#' @importFrom stats setNames
#'
#' @examples
#' tmp_dir <- tempfile("rbearcat-data-")
#' dir.create(tmp_dir)
#' csv_path <- file.path(tmp_dir, "example.csv")
#' utils::write.csv(data.frame(x = 1:3, y = letters[1:3]), csv_path, row.names = FALSE)
#'
#' datasets <- bcat_import_data(tmp_dir, "csv", utils::read.csv)
#' names(datasets)
#' datasets$example
#'
#' unlink(tmp_dir, recursive = TRUE)
bcat_import_data <- function(location, extension, import_function, recursive = TRUE) {
# Find matching files
files <- list.files(location,
pattern = paste0("\\.", extension, "$"),
recursive = recursive,
full.names = TRUE)
if (length(files) == 0L) {
warning("No files with extension '", extension, "' found in '", location, "'.",
call. = FALSE)
return(list())
}
# Derive clean names (filename without extension or path)
dataset_names <- tools::file_path_sans_ext(basename(files))
# Check uniqueness
if (anyDuplicated(dataset_names)) {
stop("File names are not unique. All files must have unique names, ",
"even if in separate subfolders. Duplicated: ",
paste(dataset_names[duplicated(dataset_names)], collapse = ", "),
call. = FALSE)
}
# Import all files
datasets <- lapply(files, import_function)
stats::setNames(datasets, dataset_names)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.