[Rd] save() saves extra stuff if object is not evaluated

Henrik Bengtsson hb at stat.berkeley.edu
Thu May 25 09:19:28 CEST 2006


Hi,

it looks like save() is saving all contents of the calling
environments if the object to be saved is *not* evaluated, although it
is not that simple either.  After many hours of troubleshooting, I'm
still confused.  Here is a reproducible example (also attached) with
output.  I let the code and the output talk for itself:

peek <- function(file, from=1, to=500) {
  cat("--------------------------------------\n")
  cat(sprintf("%s: %d bytes\n", file, file.info(file)$size))
  bfr <- suppressWarnings(readBin(file, what="character", n=to))
  bfr <- gsub("(\001|\002|\003|\004|\005|\016|\020|\036|\a|\n|\t)", "", bfr);
  bfr <- bfr[nchar(bfr) > 0];
  cat(bfr, sep="", "\n");
}

saveCache <- function(file, y, sources=NULL, eval=FALSE) {
  if (eval)
    dummy <- is.null(sources)
  base::save(file=file, sources, compress=FALSE)
}

aVariableNotSaved <- double(1e6)

main <- function() {
  # This 'big' variable is saved in case 1 below!
  big <- rep(letters, length.out=1e5)
  identifier <- "This string will be saved too!"

  y <- 1

  file <- "a.RData"
  saveCache(y, file=file)
  peek(file)

  file <- "a-eval.RData"
  saveCache(y, file=file, eval=TRUE)
  peek(file)

  file <- "b-noy.RData"
  saveCache(file=file)
  peek(file)

  file <- "b-noy-eval.RData"
  saveCache(file=file, eval=TRUE)
  peek(file)
}


# 1. Call saveCache() outside main()
eval(body(main))
# --------------------------------------
# a.RData: 238 bytes
# RDX2Xsources²filea.RData y²n $  n¦$eval¦¦¦n¦
# --------------------------------------
# a-eval.RData: 58 bytes
# RDX2Xsources¦¦
# --------------------------------------
# b-noy.RData: 230 bytes
# RDX2Xsources²file?b-noy.RData ¶yv$  n¦$eval¦¦¦n¦
# --------------------------------------
# b-noy-eval.RData: 58 bytes
# RDX2Xsources¦¦

# 2. Call saveCache() from within main()
main()
# --------------------------------------
# a.RData: 900412 bytes
# RDX2Xsources²filea.RData y² a.RData ?=identifierThis
# string will be saved too!bigåáabcdefghijklmnopqrstuv
# wxyzabcdefghijklmnopqrstuvwxyzabcdefg
# --------------------------------------
# a-eval.RData: 58 bytes
# RDX2Xsources¦¦
# --------------------------------------
# b-noy.RData: 230 bytes
# RDX2Xsources²file?b-noy.RData ¶yv$  n¦$eval¦¦¦n¦
# --------------------------------------
# b-noy-eval.RData: 58 bytes
# RDX2Xsources¦¦

What is going on?

I get this on both R v2.3.0 patched (2006-04-28 r37936) and R v2.3.1
beta (2006-05-23 r38179) on my WinXP (with Rterm --vanilla).


More information about the R-devel mailing list