R/latest.R

Defines functions latest

Documented in latest

#' @name latest
#' @title latest - An offline version control function
#'
#' This function restores the most recent version of an R object saved using the saveVersion() function.
#' @param x The variable of your R object. This must be the same variable name used to save it.
#' @keywords version control
#' @import dplyr stringr tidyr
#' @export
#' @examples
#' df <- latest("df")

latest <- function(x) {
  x <- paste0(file.path("Env","Versions",paste0(x,".rds")))

  latest_fileName <- basename(x)
  latest_fileName <- unlist(str_split(latest_fileName, "\\.") [1])[1]

  latest_version <-
    setNames(as.data.frame(
      list.files(
        file.path("Env", "Versions"),
        pattern = NULL,
        all.files = FALSE,
        full.names = FALSE,
        recursive = FALSE,
        ignore.case = FALSE,
        include.dirs = FALSE,
        no.. = FALSE
      )
    ), c("fileNames")) %>%
    separate(fileNames, c("fileName", "timestamp", "extension"), sep = "_|\\.") %>%
    # group_by(fileName) %>%
    filter(fileName == latest_fileName) %>%
    top_n(n = 1, wt = timestamp) %>%

    mutate(latest_version = paste(fileName, timestamp, sep = '_')) %>%
    mutate(latest_version = paste(latest_version, extension, sep = '.'))
  latest_file <- latest_version[1,4]

  y <- readRDS(file.path("Env","Versions",latest_file))
  assign(latest_version$fileName, y, envir = .GlobalEnv)
}
brendan-newlon/saveVersion documentation built on Dec. 11, 2019, 12:09 a.m.