Read the most recent RDS e.g. my_data_20200824.RDS vs my_data_20200825.RDS

Suppose in "./data/clean" I have few number of RDS files which end with dates e.g. my_data_20200824.RDS , my_data_20200825.RDS etc. How do I read the most recent file?

Feels like there should be a much simpler way, but this will do for a start:

test_out <- list.files(here::here("./data/clean"), pattern = "RDS$") %>%        # vector of all RDS files in dir
  dplyr::as_tibble() %>% 
  dplyr::mutate(mtime = file.mtime(here::here("./data/clean", value))) %>%      # create column on modified times
  dplyr::slice_max(order_by = mtime) %>%      # pull out the row with the most recent mtime
  dplyr::pull(value) %>%                      # pull out the filename
  here::here("./data/clean", .) %>%           # rebuild the path
  readRDS(.)                                  # and read it in
1 Like

or, if you know your filenames are all going to have that structure with no digits apart from the 8-digit ymd date:

test_out <- list.files(here::here("./data/clean"), pattern = "RDS$") %>% 
  dplyr::as_tibble() %>% 
  dplyr::mutate(mdate = stringr::str_extract(value, "[:digit:]+"))
  dplyr::slice_max(order_by = mdate) %>% 
  dplyr::pull(value) %>% 
  here::here("./data/clean", .) %>% 
  readRDS(.)

(amend regex as necessary depending on filename complexities)

1 Like

This is another option using the actual creation date of the files

library(tidyverse)

test_out <- list.files(path = "./data/clean",
                       pattern = "\\d{8}\\.RDS$",
                       full.names = TRUE) %>% 
    file.info() %>% 
    rownames_to_column("file_name") %>% 
    filter(ctime == max(ctime)) %>% 
    pull(file_name) %>% 
    readRDS()
3 Likes

Thank you both for the solution! Got what I wanted :grinning:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.