R/encode.R

Defines functions decode_obj encode_obj

Documented in decode_obj encode_obj

#' Encode an R object into hashed text
#'
#' @param obj R object
#' @param compress Compression method.
#'
#' @export
encode_obj = function(obj, compress = c("bzip2", "gzip", "xz", "none"))  {
  compress = match.arg(compress)

  raw = serialize(obj, NULL)
  comp_raw = memCompress(raw, type = compress)

  base64enc::base64encode(comp_raw)
}

#' Decode hashed text into an R object
#'
#' @param txt Hashed text.
#' @param compress Compression method.
#'
#' @export
decode_obj = function(txt, compress = c("bzip2", "gzip", "xz", "none")) {
  compress = match.arg(compress)

  if (txt == "")
    return(list())

  res = try({
    comp_raw = base64enc::base64decode(txt)
    raw = memDecompress(comp_raw, type = compress)
    unserialize(raw)
  }, silent = TRUE)

  if (inherits(res, "try-error"))
    res = list()

  res
}
rundel/learnrhash documentation built on July 21, 2022, 11 p.m.