#==============================================================================================# # FUNCTION: listFiles # Function to search through a given folder, and return the paths of the ".xlsx" files present # in that folder #----------------------------------------------------------------------------------------------# # PARAMETERS: # inputFolder : String, path of the folder containing the input files #----------------------------------------------------------------------------------------------# # RETURNS: # fileVector : String Vector, containing the relative paths of the files in the input folder #==============================================================================================# listFiles <- function(inputFolder){ inputDirectory <- file.path(inputFolder) fileVector <- list.files(path = inputDirectory, pattern = "*.xlsx", all.files = FALSE, full.names = TRUE) return(fileVector) } #==============================================================================================# # FUNCTION: readTables # Wrapper function to read tables across sheets and return them in a list # NOTE: For a given file, the logic in this code will not change. #----------------------------------------------------------------------------------------------# # PARAMETERS: # filePath : String, path of the folder containing the input files # # sheetNames : String Vector, names of the sheet in the file that contain the tables # # startRow : Integer, row from which the tables begin # # headerPresent : Boolean, denoting if the table has a header or not #----------------------------------------------------------------------------------------------# # RETURNS: # propertyInfoSheets : Dataframe List, containing the dataframes of the tables from each sheet #==============================================================================================# readTables <- function(filePath, sheetNames, startRow, headerPresent){ sheetTables <- list() for(i in seq_along(sheetNames)){ sheetTables[[i]] <- read_xlsx(path = filePath, sheet = sheetNames[i], col_names = headerPresent, skip = startRow - 1) } return(sheetTables) } #==============================================================================================# # FUNCTION: readTables # Wrapper function to collate the tables present in a list into a single table #----------------------------------------------------------------------------------------------# # PARAMETERS: # tableList : Dataframe List, containing the tables to be collated #----------------------------------------------------------------------------------------------# # RETURNS: # collatedTable : Dataframe, collated from the given list #==============================================================================================# collateTables <- function(tableList){ collatedDataFrame <- data.frame() for(i in seq_along(tableList)){ if(i == 1){ collatedDataFrame <- tableList[[i]] } else{ collatedDataFrame <- as.data.frame(rbind(collatedDataFrame, tableList[[i]])) } } return(collatedDataFrame) } #==============================================================================================# # FUNCTION: dropColumns # Wrapper function to iterate through the tables in a sheet and retain only the required # columns #----------------------------------------------------------------------------------------------# # PARAMETERS: # tableList : Dataframe List, containing the tables to be collated # # columnsToRetain : Character Vector, containing the columns to be retained from the tables #----------------------------------------------------------------------------------------------# # RETURNS: # treatedTableList : Dataframe List, containing the tables to be collated with only the required # columns #==============================================================================================# dropColumns <- function(tableList, columnsToRetain){ for(i in seq_along(tableList)){ tableList[[i]] <- tableList[[i]][, columnsToRetain] } return(tableList) }