library(readxl)
library(tidyverse)
library(here)
library(janitor)
source(here("scripts","final", "clean_write_function.R"))
Solution from https://stackoverflow.com/questions/47540471/load-multiple-excel-files-and-name-object-after-a-file-name
filepath <- here("data", "dirty")
file_list <- list.files(path = filepath, pattern='*.xlsx')
This option reads the dirty xlsx files into a list, runs clean_write on that list, which returns exported clean csv files and a list with clean data in each list item. How…?
the map function takes a list (in this case the list of files) and runs a function (in this case read_excel + mutate(filename)) across all of them.
map will return a list, in this case a list containing each of the read in excel files + a filename column as a separate item.
purrr loves lists, once all the files are read in as list items, can use map again (or maybe even map_df- which returns a dataframe- see option 2) to run cleanwrite_emg across all items in the list?
updated 21/5/2020 advice from Jenny Bryan, option 1 https://jenrichmond.github.io/map_excel_reprex/
list_raw_emg <- file_list %>%
map(~ read_excel(path = here("data", "dirty", .x), range = "A4:AE11") %>%
mutate("file_name" = .x))
# get first item of list for testing- looks good
rawemg1 <- list_raw_emg[[1]]
Run cleanwrite_emg on list of files using map, returns a list. Each item in the list is a cleaned file AND each is written to csv in the clean_data folder.
list_cleanwrite_emg <- list_raw_emg %>%
map(~ cleanwrite_emg(.x))
did you get what you want? look at one item from the cleanemg emglist. There should also be clean csv files in your folder.
cleanemg159 <- list_cleanwrite_emg[[159]]
YES!