If I have a bunch of Excel sheets in a folder and all contain same data except it gets updated every month with same name ending with month & year.
Instead of reading exact file with its name using
read_excel("\Folder\file_202305.xlsx").
Is there a tidyverse way that file ending with latest year & month gets picked up without specifying the full file name?
It would also be great to know, if we had different names all together, but just based on latest updated date, we can read the latest file regardless of its name.
library(readxl)
# Option2 with systematic file names
Files <- list.files("Excel Files/", pattern = "xlsx$")
MaxFile <- max(Files)
# We do get the latest file here. But while reading the file like below, we get the error
ExcelData <- read_xlsx(MaxFile)
# Error: `path` does not exist: ‘Test Data_202305.xlsx’
# Path is already listed above as "Excel Files/"
## Option1 - File with most recent date of modification
Files <- list.files("Excel Files/", pattern = "xlsx$")
INFO <- file.info(Files)
# INFO shows blank columns for each excel file. So, mtime is NA
MaxTime <- max(INFO$mtime)
# MaxTime is thus, blank
LastFile <- rownames(INFO[INFO$mtime == MaxTime,])
# LastFile shows "NA", "NA.1" because of blank columns in INFO
ExcelData <- read_xlsx(LastFile)
# Same error of Path doesn't exist as in Option2
The read_xlsx() function is looking for the file in the working directory and not in Excel_Files/. Setting the path argument in list.files() does not set the path in other functions. To construct the correct file name, you can use code like