Created
November 29, 2016 18:41
-
-
Save tjmahr/2866e4d35d2be316ec3ea244b0e8eacb to your computer and use it in GitHub Desktop.
reading files with purrr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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