-
Notifications
You must be signed in to change notification settings - Fork 12
/
encode.R
38 lines (31 loc) · 821 Bytes
/
encode.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#' 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
}