R/r_utils.R

Defines functions ascii_code_to_str str_to_ascii_code parse_url pkg_name to_str.numeric to_str.list to_str.default to_str paws_error_code get_aws_env `%||%`

#' @import R6
#' @import sagemaker.core
#' @importFrom utils getFromNamespace help
#' @importFrom urltools url_parse

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

get_aws_env <- function(x) {
  x <- Sys.getenv(x)
  if (nchar(x) == 0) return(NULL) else return(x)
}

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

to_str <- function(obj, ...){
  UseMethod("to_str")
}

to_str.default <- function(obj, ...){
  as.character(obj)
}

to_str.list <- function(obj, ...){
  jsonlite::toJSON(obj, auto_unbox = F)
}

to_str.numeric <- function(obj, ...){
  format(obj, scientific = F)
}

# Correctly mimic python append method for list
# Full credit to package rlist: https://github.com/renkun-ken/rlist/blob/2692e064fc7b6cc7fe7079b3762df37bc25b3dbd/R/list.insert.R#L26-L44
list.append = function (.data, ...) {
  if (is.list(.data)) c(.data, list(...)) else c(.data, ..., recursive = FALSE)
}

pkg_name = function(){
  env <- topenv(environment())
  get0(".packageName", envir = env, inherits = FALSE)
}

parse_url = function(url){
  url = ifelse(is.null(url) | is.logical(url) , "", url)
  url = ifelse(grepl("/", url), url, sprintf("/%s", url))
  urltools::url_parse(url)
}

# ascii code converter developed from:
# https://www.r-bloggers.com/2011/03/ascii-code-table-in-r/
str_to_ascii_code <- function(str) {
  lapply(str, function(x) strtoi(charToRaw(x),16L))
}

ascii_code_to_str <- function(obj){
  vapply(obj, function(x) {rawToChar(as.raw(x))}, FUN.VALUE = character(1))
}
DyfanJones/sagemaker-r-common documentation built on June 14, 2022, 10:31 p.m.