Search code examples
rdatedplyrtidyrdata-manipulation

import dataset automaticly in R


I have a dataset my.df <- read_excel("C:/Users/Seyma/Docs/DF_05_04_2023.xlsx") that belongs the day before. Each day I receive the same dataset based on the previous day (except on weekends and Italian national holidays). I would like to update the my.df without changing the directory manually (such as 05_05_2023). is there a way to get around this? many thanks in advance.


Solution

  • You can use Sys.Date and strftime to generate the file name:

    strftime(Sys.Date() - 1, 'C:/Users/Seyma/Docs/DF_%m_%d_%Y.xlsx')
    #> [1] "C:/Users/Seyma/Docs/DF_05_04_2023.xlsx"
    

    Tomorrow, the same code will produce "C:/Users/Seyma/Docs/DF_05_05_2023.xlsx"

    So an automated version of your code would be

    my.df <- strftime(Sys.Date() - 1, 'C:/Users/Seyma/Docs/DF_%m_%d_%Y.xlsx') |>
               read_excel()