[R] Raw data type transformations

dhinds@sonic.net dhinds at sonic.net
Wed Aug 17 20:39:08 CEST 2005


Not sure if this belongs here or on the devel list:

I've needed a more efficient way to manipulate raw binary data in R,
with more than the minimal raw transformation functions in the base
package.  So I've written a small package in C that effectively lets
me cast back and forth between raw vectors and other data types.

I've implemented four functions: rawToHex, hexToRaw, readRaw, and
writeRaw.  The first two convert between raw vectors and hex character
strings.  I use these to work around a limitation of ROracle not
directly supporting raw data types, but they may have other uses.
readRaw and writeRaw are similar to readBin and writeBin, but operate
on raw vectors in place of binary connections, and I took a few
shortcuts in my implementation (I handle a subset of data types and
didn't implement byte swapping).

[aside: Rinternals.h does not export RAW() for packages]

I first thought there might be a way to do this via connections but
didn't want to actually read and write files just to map between data
types.  One option would be to implement a rawConnection() analog of
the current textConnection(), which would associate raw vectors with
binary connections, and allow readBin()/writeBin().

Looking at the text connection code, it seems to me that it could be
extended to handle this fairly easily.  For input connections, it
appears that all that needs to be done is to add support for filling
the internal buffer from a raw vector (a few lines of code), and
adding a text_read() function (again just a few lines).  For output,
text_write() is similarly simple, and a few lines of code would need
to be added to text_vfprintf() to handle copying to a raw vector in
place of a character vector.

The text_* functions would then probably be better named internal_*
since they would handle internal binary as well as internal text
connections.

Does this sound like it would be a useful capability?

-- David Hinds




More information about the R-help mailing list