I'm currently using this handmade thing (which should of course be adjusted for different data types):
read_blob <- function(blob) {
blob_con <- rawConnection(blob)
data <- readBin(blob_con, "numeric", n = length(blob) / 4)
close(blob_con)
return(data)
}
write_blob <- function(data) {
stopifnot(is.numeric(data))
blob_con <- rawConnection(raw(0), "r+")
writeBin(data, blob_con)
blob <- rawConnectionValue(blob_con)
close(blob_con)
return(blob)
}
input_data <- c(123.45,234.56,345.67)
data_to_raw <- write_blob(input_data)
print(data_to_raw)
raw_to_data <- read_blob(data_to_raw)
print(raw_to_data)
stopifnot(all(input_data == raw_to_data))
I consider this somewhat of a workaround, having to open a rawConnection for reading and writing. Is there really no better way of doing this?
I know of
rawToChar
but no other data types,- the package
pack
which should work with structure descriptors in the style ofunpack('d*', data)
. I guess it was never finished because in my case only returns the first value regardless of what I put in the pattern, and last updated in 2008. - the package
blob
which doesn't seem to be doing this on its own.