close Warning: Can't synchronize with repository "(default)" (/var/svn/tolp does not appear to be a Subversion repository.). Look in the Trac log for more information.

Ticket #1400: TOLutils.r

File TOLutils.r, 1.6 KB (added by Jorge, 13 years ago)
Line 
1################################################################################
2## FILE: TOLutils.R
3## PURPOSE: Communication API between TOL and R
4## AUTHOR: josp@tol-prj.org
5##
6## ACKNOWLEDGEMENTS:
7##
8##  R Development Core Team (2007). R: A language and environment for
9##  statistical computing. R Foundation for Statistical Computing, "
10##  Vienna, Austria. ISBN 3-900051-07-0, \n"
11##  URL http://www.R-project.org.\n"};
12##
13################################################################################
14
15################################################################################
16read.bbm <-
17################################################################################
18function(fbbm) {
19  fd = file(fbbm, open='rb')
20  dim <- readBin(fd,integer(), n=2)
21  data<-readBin(fd,double(),n=dim[1]*dim[2])
22  close(fd)
23  matrix(data, nrow=dim[1], ncol=dim[2], byrow=TRUE)
24}
25
26################################################################################
27write.bbm <-
28################################################################################
29function(fbbm, mat) {
30  fd = file(fbbm, open='wb')
31  if (is.matrix(mat)) {
32    writeBin(dim(mat), fd)
33    writeBin(as.numeric(t(mat)), fd)
34  } else if (is.vector(mat)) {
35    Z <- matrix(mat,length(mat), 1)
36    writeBin(dim(Z), fd)
37    writeBin(as.numeric(t(Z)), fd)
38  } else {
39    writeBin(c(0,0), fd)
40  }
41  close(fd)
42}
43
44################################################################################
45get.tol.arg <-
46################################################################################
47function(value, def)
48{
49  if (is.null(value))
50    def
51  else
52    value
53}