[Rd] save() size in XDR

Duncan Murdoch dmurdoch at pair.com
Thu Feb 26 20:58:27 MET 2004


On Thu, 26 Feb 2004 19:31:15 +0000 (GMT), Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote :

>On Thu, 26 Feb 2004, Duncan Murdoch wrote:

>> One way to solve this problem would be to create a connection that did
>> nothing except keep track of a file position, then do the save to that
>> connection.  However, it's not easy to define new connection types.
>> Might be a nice package to write to allow such a thing.
>
>I don't think it is worth it.  Just use an anonymous file connection and 
>seek to the end to measure the length.  (Similar to Luke's use of a pipe, 
>but more portable.)

I didn't mean to write a package to count bytes, I meant to write a
package that allowed for user-defined connections.  The various
functions (read, write, seek, etc.) needed by the connection would be
supplied by the user in R code.

Then you'd do something like this for the special case of counting
bytes:

count <- 0

countbytes <- function(num, bytes) {
   count <<- count + num
}

f <- userconnection(write=countbytes)

save(list=ls(all=TRUE),file=f)

cat('Byte count is ',count)

Other possible connections you might want:

Circular connections that cycle through a buffer of bytes again and
again.

Encrypting connections which do something to the bytes on their way to
another connection.

Compressing connections which compress or uncompress another
connection.

Bit-level connections that let you read or write one bit at a time.

Concatenating connections that join two inputs to make them look like
a single stream.

Limiting connections that only show the caller a subset of the stream,
within a certain byte range.

Connections that calculate a checksum or CRC on the bytes as they pass
through.

Many years ago I wrote a Turbo Pascal unit containing all of these
things for "streams", the TP equivalent of connections, and it ended
up being surprisingly useful.

Duncan Murdoch



More information about the R-devel mailing list