Read binary vector

狂风中的少年 提交于 2021-02-11 04:58:34

问题


Imagine, I get a binary response from an API, which is a .rds object created with saveRDS() (R-version > 3.5). Basically the first five lines of the built in iris data set, like the cont object below:

cont = as.raw(c(0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
            0x03, 0x85, 0x50, 0xb1, 0x4e, 0xc3, 0x30, 0x10, 0x75, 0x93, 0xba, 
            0x6d, 0x42, 0x40, 0xa8, 0xed, 0x6f, 0x90, 0x99, 0xad, 0x9e, 0x98, 
            0x18, 0x90, 0x0a, 0x82, 0xd5, 0x72, 0x9d, 0x62, 0xc9, 0x24, 0x95, 
            0x6d, 0x85, 0x95, 0xdf, 0x80, 0x3f, 0x42, 0x82, 0xbf, 0x41, 0x88, 
            0x09, 0xb0, 0x73, 0xe7, 0x08, 0xd2, 0xa1, 0x27, 0x59, 0xef, 0xee, 
            0xf9, 0xbd, 0x3b, 0xdd, 0xdd, 0xe5, 0x84, 0x90, 0x94, 0xa4, 0x93, 
            0x11, 0x49, 0xa9, 0x4f, 0x09, 0xbd, 0xb9, 0xbe, 0x38, 0x3b, 0xf7, 
            0xdc, 0x22, 0x14, 0xfe, 0x9d, 0x04, 0x64, 0xcb, 0xaa, 0x0b, 0xb6, 
            0x78, 0x0e, 0xf1, 0xc2, 0xe6, 0x6f, 0x21, 0xde, 0xd9, 0x1c, 0xf9, 
            0x25, 0x89, 0x01, 0xfa, 0x02, 0x0a, 0x36, 0x43, 0xcc, 0xd0, 0x37, 
            0x43, 0x5f, 0x01, 0x18, 0xf5, 0xab, 0x4f, 0xe8, 0xd3, 0xe3, 0x07, 
            0xfc, 0xaf, 0xbe, 0xc0, 0x1f, 0xf9, 0x5e, 0xff, 0x0a, 0xfd, 0x0e, 
            0xa1, 0xdf, 0xe3, 0x18, 0xf7, 0x18, 0xed, 0xbf, 0x71, 0xd2, 0xe1, 
            0x98, 0x64, 0x1e, 0x27, 0x5a, 0xb6, 0x52, 0x5b, 0x9f, 0x9d, 0x76, 
            0x27, 0x41, 0xd6, 0x4a, 0xd7, 0x58, 0x8e, 0x55, 0xde, 0x4a, 0x63, 
            0x95, 0x68, 0x74, 0x63, 0x90, 0xc9, 0x5a, 0x65, 0xb6, 0xaa, 0x56, 
            0x82, 0x0f, 0xda, 0x51, 0xa1, 0xb9, 0x8d, 0xdd, 0xfa, 0x19, 0x15, 
            0x17, 0xce, 0x7b, 0x09, 0xf9, 0x1e, 0xca, 0x6b, 0xfe, 0x20, 0xa3, 
            0x9c, 0x22, 0x59, 0xac, 0xe5, 0x8e, 0xeb, 0xf2, 0x52, 0xd6, 0x5b, 
            0x77, 0x8f, 0xdc, 0x11, 0x70, 0xb7, 0x6a, 0xd3, 0x53, 0xc5, 0x95, 
            0x74, 0x7b, 0x32, 0xe0, 0xfe, 0xca, 0xa6, 0xeb, 0x9d, 0x14, 0x2a, 
            0x0c, 0xf9, 0x37, 0x39, 0x33, 0xcd, 0x63, 0x19, 0xa7, 0x87, 0x63, 
            0x25, 0x4f, 0x70, 0x73, 0x0a, 0xc2, 0xe4, 0x67, 0xb0, 0x44, 0xbe, 
            0xe1, 0x8e, 0x97, 0x95, 0xf1, 0x96, 0xb0, 0xc8, 0x2f, 0xd6, 0xf2, 
            0xca, 0x74, 0x42, 0x02, 0x00, 0x00))

I can save it locally and the read it with readRDS() as such:

tmp = tempfile()
writeBin(cont, tmp)
iris5 = readRDS(tmp)

However, isn't there a way in R to directly read the binary vector back into a data.frame? Something like readBin(cont) - though this doesn't work.


回答1:


You can use rawConnection() to convert the raw vector to a readable connection. A complication is that your bytes are compressed, and when reading from a connection, readRDS() won't automatically uncompress them. So you'll get this:

readRDS(rawConnection(cont))
# Error in readRDS(rawConnection(cont)) : unknown input format

However, you can explicitly decompress the file, and then it works:

readRDS(gzcon(rawConnection(cont)))
#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1          5.1         3.5          1.4         0.2  setosa
#2          4.9         3.0          1.4         0.2  setosa
#3          4.7         3.2          1.3         0.2  setosa
#4          4.6         3.1          1.5         0.2  setosa
#5          5.0         3.6          1.4         0.2  setosa

Edited to add: One other complication is that the code above opens the connection, but never closes it, so eventually you'll get warnings about unused connections being left open. This fixes that problem:

con <- gzcon(rawConnection(cont))
result <- readRDS(con)
close(con)

You only need one close() here, because when the gzcon connection is closed, it will close the underlying rawConnection.



来源:https://stackoverflow.com/questions/58135794/read-binary-vector

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!