Re: [S] source(For()) works (almost) in S-Plus v4.0

Prof Brian Ripley (
Fri, 19 Jun 1998 10:06:22 +0100 (BST)

Content-Type: TEXT/plain; charset=us-ascii
Content-MD5: 8QrOLvY8gV/YfYs2YITzkA==

> Date: Thu, 18 Jun 1998 14:27:32 -0700 (PDT)
> From: Steve Edland <>
> For loops apparently don't work in S-Plus v4.0, Windows 95.
> I have found that sourcing the output of a call to For(), e.g.,:
> source(For(i=1:10,x[i]_foo()))
> works just fine. The only problem is you have to exit S-plus and
> get back in before you can continue. It seems that I am very close to a
> complete work-around. Can anyone take it the next step? I would like
> to be able to submit a batch file with multiple For loops.

That is the intended behaviour of the output of For(). You really don't
want to source it: it is intended to be run as the sole input of a
separate S-PLUS process. If you do want to run the script separately,
remove the lines setting .Program etc. But I usually find it better to
unroll a large loop into a series of smaller for loops: see V&R2
section 4.10 and the programming complements for tricks of this sort.

I looked in my files and found the file For.q attached. This does
more-or-less work correctly on S-PLUS 4.0r3, and allows you to
execute For() loops within S-PLUS (or Sqpe). The default is exec=T.
You can't use graphsheet or win.printer inside this version of For().


(a) SHOME must be set in the global environment. For Win95 users,
that means setting it in autoexec.bat: for WinNT use the control panel.
(This is a nuisance for those of us who regularly use several
versions of S-PLUS.)

(b) SHOME\cmd must be in the path in the global environment or set within
For (as in the attached).

(c) S-PLUS4 does get its summary files confused when more than one
process is running, so you may get messages about being unable to
write summaries and need to delete _Data/__sum4*.

> Steve Edland
> Alzheimer's Disease Research Center
> University of Washington
> (An earlier posting describing the problem is attached below. Joel
> Damiani says that the problem has been fixed in v4.5. That doesn't
> solve my immediate problem, since the University of Washington site
> licence is for v4.0.)

Actually, what they have done is very similar to my earlier fix, and
has exactly the same problems (certainly (a) and (b) and I believe (c),
but the precise cause of that is hard to pin down). The only
difference is that there is an option to run Splus /BATCH not Sqpe,
and only S-PLUS 4.5 allows the running of multiple copies (AFAIK).
The help page for For has _still_ not been updated.

(i) Is it not about time Sqpe.exe could find SHOME in the registry?
The error message says it could not be found there, but it is there
(and Splus.exe finds it). This was about to be fixed in the very first
beta of 4.0!
(ii) I think Splus /UTILITY will allow you to launch sqpe with SHOME
set. Perhaps it also could add SHOME\cmd to the search path (if it
does not do this already). Then you could use this to launch sqpe
inside For and avoid (a) and (b).
(iii) Please update the help page for For. (I am tired of reporting
this bug repeatedly.)

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

--Flight_of_Pigeons_829_000 Content-Type: TEXT/plain; name="For.q"; charset=us-ascii; x-unix-mode=0640 Content-Description: For.q Content-MD5: wTCQD3XlYTIilubZjAnNmw==

For <- function(...) { call <- cnames <- names(call)[-1] if(is.null(cnames)) cnames <- character(length(call) - 1) if(length(call) < 3) stop("must supply two arguments (steps, expression)") WAIT <- TRUE SYNC <- TRUE PROG <- "/packages/splus4/cmd/Sqpe" #set as appropriate QUIT <- TRUE FIRST <- NULL GRAIN.SIZE <- 1 DEBUG <- FALSE EXEC <- TRUE if(length(call) > 3) { <-, wait, sync, prog, quit, grain.size, debug, exec) NULL, call[ - c(2, 3)]) if(!is.null(expr <-$wait)) WAIT <- eval(expr, sys.parent()) if(!is.null(expr <-$sync)) SYNC <- eval(expr, sys.parent()) if(!is.null(expr <-$quit)) QUIT <- eval(expr, sys.parent()) if(!is.null(expr <-$prog)) PROG <- eval(expr, sys.parent()) if(!is.null(expr <-$first)) FIRST <- deparse(expr) if(!is.null(expr <-$grain.size)) GRAIN.SIZE <- eval(expr, sys.parent()) if(!is.null(expr <-$debug)) DEBUG <- eval(expr, sys.parent()) if(!is.null(expr <-$exec)) EXEC <- eval(expr, sys.parent()) } steps <- eval(call[[2]], sys.parent()) stepname <- cnames[1] if(nchar(stepname)) assign(".Steps", steps, w = 1, immediate = T) resultname <- cnames[2] steps <- seq(along = steps) cmd <- paste(deparse(call[[3]]), collapse = "\n") if(nchar(resultname)) cmd <- paste(resultname, "[[", steps, "]] <- ", cmd, sep = "") if(nchar(stepname)) cmd <- paste(stepname, " <- .Steps[[", steps, "]]; ", cmd, sep = "") else if(!nchar(resultname)) cmd <- rep(cmd, length = length(steps)) lh <- rep(" ", length(cmd)) lh[seq(1, length(lh), by = GRAIN.SIZE)] <- "{" rh <- rep("", length(cmd)) rh[seq(GRAIN.SIZE, length(rh), by = GRAIN.SIZE)] <- "\n}" rh[length(rh)] <- "\n}" cmd <- paste(lh, cmd, rh, sep = "") file <- tempfile("Iter") keep.file <- FALSE if(WAIT) on.exit(if(!keep.file) unlink(file)) if(apnd <- QUIT) { cur.err <- options(error = NULL) on.exit(options(cur.err), add = T) new.err <- function() { NULL q(1) } # assumes options()$error is an expression or a 0-arg. function new.err[[1]][[1]] <- cur.err[[1]][[1]] dput(substitute(options(error = err), list(err = new.err)), file = file) } if(length(FIRST) > 0) { cat(paste(FIRST, collapse = "\n"), "\n", sep = "", file = file, append = apnd) apnd <- TRUE } cat("assign('.Program', w=0, expression(eval(parse())))\n", if(nchar( stepname)) "assign('.Steps', .Steps, w=0) ; remove('.Steps', w=1)\n" else "", cmd, file = file, sep = "\n", append = apnd, fill = T ) if(!EXEC) { keep.file <- TRUE return(file) } if(!WAIT) cat("unlink(\"", file, "\")\n", file = file, append = T, sep = "") if(SYNC) synchronize() if(DEBUG) page(file = file) if(WAIT) { res <- dos(paste(PROG, "<", file), translate=T, minimize=T, synchronize(1) } else { dos(paste(PROG, "<", file, "> tmp.out"), translate=T,, multi=T) cat("running Sqpe in an MS-DOS window, output to tmp.out\n") } invisible(NULL) }

--Flight_of_Pigeons_829_000-- ----------------------------------------------------------------------- This message was distributed by To unsubscribe send e-mail to with the BODY of the message: unsubscribe s-news