Skip to content

Instantly share code, notes, and snippets.

@tjmahr
Created November 29, 2016 18:41
Show Gist options
  • Select an option

  • Save tjmahr/2866e4d35d2be316ec3ea244b0e8eacb to your computer and use it in GitHub Desktop.

Select an option

Save tjmahr/2866e4d35d2be316ec3ea244b0e8eacb to your computer and use it in GitHub Desktop.
reading files with purrr
my_dir <- tempdir()
readr::write_csv(iris, file.path(my_dir, "iris1.csv"))
readr::write_csv(iris, file.path(my_dir, "iris2.csv"))
readr::write_csv(iris, file.path(my_dir, "iris3.csv"))
readr::write_csv(iris, file.path(my_dir, "iris4.csv"))
file_paths <- list.files(my_dir, pattern = "iris", full.names = TRUE)
# Force an error so safely() does something
file.remove(file.path(my_dir, "iris3.csv"))
library(purrr)
results <- file_paths %>%
set_names(basename(.)) %>%
map(safely(readr::read_csv))
data <- results %>%
map_df("result", .id = "File")
data
errors <- results %>%
map("error") %>%
compact()
errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment