[R] Compression data to variable using gzcon
Jan van der Laan
rhe|p @end|ng |rom eoo@@dd@@n|
Fri Jan 31 16:43:38 CET 2025
I wanted to compress a raw vector into another raw vector. This problem
is solved using memCompress. However before discovering this function, I
first tried to get this working using gzcon. From the documentation it
seems that this should be possible:
> Compressed output will contain embedded NUL bytes, and so ‘con’ is
> not permitted to be a ‘textConnection’ opened with ‘open = "w"’.
> Use a writable ‘rawConnection’ to compress data into a variable.
But I can't seem to get this to work:
> data <- as.raw(sample(0:255, 100))
> con <- rawConnection(raw(0), "wb")
> zz <- gzcon(con)
> writeBin(data, zz)
> val <- rawConnectionValue(con)
Error in rawConnectionValue(con) :
internal connection is not a rawConnection
The documentation of gzcon does mention that the original connection
becomes unusable. Ok, so this error is to be expected. But how do I get
to the compressed data?
As I mentioned memCompress(data) does what I want, so the the problem of
compressing the data itself is solved, but I would like to understand
how to use gzcon.
Thanks.
Jan
More information about the R-help
mailing list