R/r_utils.R

Defines functions list.exist.in empty paws_error_code .set list.append enum_items `%||%`

`%||%` = function(x, y) if (is.null(x)) return(y) else return(x)

enum_items = function(enum) unlist(as.list(enum), use.names = F)

list.append = function(.data, ...) {
  if (is.list(.data)) c(.data, list(...)) else c(.data, ..., recursive = FALSE)
}

# Sets dictionary[key] = value if value is not None.
.set = function(value, key, dictionary){
  if (!is.null(value)) {
    temp_dict = deparse(substitute(dictionary))
    dictionary[[key]] = value
    assign(temp_dict, dictionary, envir = parent.frame())
  }
}

paws_error_code = function(error){
  return(error[["error_response"]][["__type"]] %||% error[["error_response"]][["Code"]])
}

empty = function(obj){
  is.null(obj) || !(length(obj) == 0 || nzchar(obj))
}

# Checks if a list exists with in another list.
#' @import data.table
list.exist.in = function(a, b){
  main_list = rbindlist(b, fill = TRUE)
  if (length(names(main_list)) == 0) return(FALSE)
  sub_list = as.data.table(a)
  if(!all(names(sub_list) %in% names(main_list))) return(FALSE)
  setcolorder(main_list, names(sub_list))
  set(main_list, j="check", value=FALSE)
  main_list[sub_list, "check" := TRUE, on = names(sub_list)]
  return(any(main_list$check))
}
DyfanJones/sagemaker-r-workflow documentation built on April 3, 2022, 11:28 p.m.