R/utils.R

Defines functions is_negative is_positive contain_all_keywords contain_any_keywords cleanup backup print_with_time

Documented in contain_all_keywords contain_any_keywords print_with_time

#' print_with_time
#'
#' @param message string
#'
#' @return NULL
#' @export
print_with_time <- function(message) {
  if (message == "=") {
    message <- paste0(rep("=", 80), collapse = "")
  } else if (message == "-") {
    message <- paste0(rep("-", 80), collapse = "")
  } else {
    message
  }
  now <- format(Sys.time(), "%Y-%m-%d %H:%M:%S")
  cat(sprintf("[%s] %s\n", now, message))
}

# Backup files
backup <- function(dir) {
  old <- setwd(dir)
  on.exit(setwd(old), add = TRUE)

  dir_nm <- format(Sys.Date(), "%Y%m%d")
  dir.create(file.path("./backup", dir_nm), showWarnings = FALSE, recursive = TRUE)

  file_nm <- list.files(".", "zip$|csv$|sqlite$")
  file.rename(from = file.path(".", file_nm), to = file.path("./backup", dir_nm, file_nm))
}

# Clean files in dir with specified file format
cleanup <- function(dir, pattern = "*") {
  old <- setwd(dir)
  on.exit(setwd(old), add = TRUE)

  files <- list.files(".", pattern = pattern)
  file.remove(files)
}

#' contain_any_keywords
#'
#' @param x object
#' @param ... keywords
#'
#' @return logical
#' @export
contain_any_keywords <- function(x, ...) {
  any(c(...) %in% x)
}

#' contain_all_keywords
#'
#' @param x object
#' @param ... keywords
#'
#' @return logical
#' @export
contain_all_keywords <- function(x, ...) {
  all(c(...) %in% x)
}

is_positive <- function(x) {
  stopifnot(is.numeric(x))
  x >= 0
}

is_negative <- function(x) {
  stopifnot(is.numeric(x))
  x < 0
}
chinhungtseng/taitratools documentation built on Jan. 11, 2021, 8:33 p.m.