I’m trying to write an HTTP POST request, but I need to get my data into binary format first. This is probably an easy question, but I find R connections really confusing, and I’ve been having trouble finding a good resource that explains them in a way I understand.
So as an example, say I want to encode an integer (8 byte) and then a numeric (4 byte). Here is the code I’ve tried:
myint <- as.integer(1339700942)
mydouble <- 1.2
obj <- file(open='w+b') #I've tried textConnection too, but no good
w.int <- writeBin(myint, obj, size=8, endian='big')
w.double <- writeBin(mydouble, obj, size=4, endian='big')
This allowed me to open the connection, but all it wrote was NULL. What is the correct way to use connections and writeBin in a situation like this?
The object ‘obj’ is not actually necessary. If writeBin is writing to an R object rather than a file outside of R, the last three lines can be replaced by: