| chain.write {tripEstimation} | R Documentation |
~~ A concise (1-5 lines) description of what the function does. ~~
chain.write(filename, A, append = FALSE)
filename |
~~Describe filename here~~ |
A |
~~Describe A here~~ |
append |
~~Describe append here~~ |
~~ If necessary, more details than the description above ~~
~Describe the value returned If it is a LIST, use
comp1 |
Description of 'comp1' |
comp2 |
Description of 'comp2' |
...
....
~~further notes~~
~Make other sections like Warning with section{Warning }{....} ~
~~who you are~~
~put references to the literature/web site here ~
~~objects to See Also as help, ~~~
##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function(filename,A,append=FALSE) {
A <- as.array(A)
if(!append) {
## Open a binary connection for writing, truncating or
## creating the file if necessary
con <- file(filename,open="wb")
## Write array dimensions
writeBin(as.integer(dim(A)),con)
## Write the data and close
writeBin(as.double(A),con)
close(con)
} else {
## Open a binary connection for appending
con <- file(filename,open="r+b")
## Read the dimensions of the array
seek(con,0,origin="start",rw="r")
dm <- readBin(con,"integer",3)
## Check the data to append has compatible dimensions
if(length(dm)!=length(dim(A)) || any(dm[-3]!=dim(A)[-3]))
stop("Incompatible dimensions for appending\n")
## Append the new data
seek(con,0,origin="end",rw="w")
writeBin(as.double(A),con)
## Write new dimensions and close
dm[3] <- dm[3]+dim(A)[3]
seek(con,0,origin="start",rw="w")
writeBin(as.integer(dm),con)
close(con)
}
}