library(testthat) library(dplyr) library(readxl) library(purrr) pkgload::load_all() read_excel('inst/annee_2012.xlsx') read_excel('inst/annee_2009.xlsx') # possi_excel <- possibly(read_excel, otherwise = NULL, quiet = TRUE) # possi_excel('data/annee_2009.xlsx') # # # files <- list.files(path = 'inst/', full.names = TRUE) # files %>% # map(possi_excel) # # # # multi_read_excel <- function(folder = "data/"){ # # files <- list.files(folder, full.names = TRUE, pattern = '[.]xlsx$') # data <- files %>% # map(possi_excel) # return(data) # # } # multi_read_excel(folder = "inst/") # # # multi_read_excel <- function(folder = "data/"){ # # files <- list.files(folder, full.names = TRUE, pattern = '[.]xlsx$') # filenames <- list.files(folder, pattern = '[.]xlsx$') # data <- files %>% # map(possi_excel) # names(data) <- filenames # # return(data) # # } # # multi_read_excel(folder = "inst/")
#' my_fun A function to read an excel file and return NULL if there is a problem with the file #' #' @param ... pathfile (type = character) #' #' @importFrom purrr possibly #' @importFrom readxl read_excel #' @return A data frame when the file is read successfully, or NULL if there is an error #' @export possi_excel <- possibly(read_excel, otherwise = NULL, quiet = TRUE)
possi_excel('inst/annee_2009.xlsx') possi_excel('inst/annee_2010.xlsx')
test_that("possi_excel works", { expect_null(possi_excel('inst/annee_2009.xlsx')) expect_type(possi_excel('inst/annee_2010.xlsx'), "list") })
#' my_fun A function to read all available excel files in a folder #' #' @param folder A path to a folder (type = character) #' #' @importFrom purrr possibly map #' @importFrom readxl read_excel #' @return A list of data frames, after reading all available excel files in a folder #' @export multi_read_excel <- function(folder = "data/"){ files <- list.files(folder, full.names = TRUE, pattern = '[.]xlsx$') filenames <- list.files(folder, pattern = '[.]xlsx$') data <- files %>% map(possi_excel) names(data) <- filenames return(data) }
multi_read_excel(folder = "inst/")
test_that("multi_read_excel works", { expect_type(multi_read_excel('inst/'), "list") expect_named(multi_read_excel('inst/'), list.files("inst/")) })
# Run but keep eval=FALSE to avoid infinite loop # Execute in the console directly fusen::inflate(flat_file = "dev/flat_minimal.Rmd", vignette_name = "Minimal")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.