R/prototyping/prototyping.R

Defines functions date_out_of_range

fp <- here::here("R", "prototyping")
library(magrittr)
all_f <- fp %>% list.files()
my_f <- c(script1 = "test_script",
          script2 = "testme",
          r_file = "test_script.R",
          r_file2 = "test_script.Rmd",
          r_file3 = "test_script_test_script.R",
          r_file4 = "01_test_script.R",
          r_file5 = "1_test_script.R",
          r_file6 = "1test_script.R",
          r_file7 = "111_test_script.R")
#Rename file according to index
which(my_f == "testme", arr.ind = TRUE) %>% as.numeric()

#match original name
matchme <- grepl(paste0("^", my_f[1], ".R$"), my_f)
ori_match <- my_f[matchme]
rname <- all_f[all_f %in% ori_match]
file.rename(paste0(fp, "/", rname), paste0(fp, "/", "new_name.R"))


#Match new name
grepl(paste0("^[0-9][0-9][0-9]", "_", my_f[1], ".R$"), my_f)


#anything not this format called out
#anything not in sequential order also called out
grepl(paste0("^[0-9][0-9][0-9]", "_", '*', ".R$"), my_f)

# -------------------------------------------------------------------------
#This function is used to identify date values outside
#a specified date range.
date_out_of_range <- function(id_column, date_column, date_max_cutoff,
                                  date_min_cutoff) {
  #strip datetime
  nm_id_col <- deparse(substitute(id_column))
  nm_date_col <- deparse(substitute(date_column))

  my_data <- cbind(id_column, date_column) %>% as.data.frame()
  colnames(my_data) <- c("id_col", "date_col")

  far_dates <- dplyr::filter(my_data, date_col > date_max_cutoff |
                               date_col < date_min_cutoff)
  return(far_dates)
}
virusgeeks/vgtools documentation built on April 25, 2022, 12:38 p.m.