[R] Command line R / PHP?

Martin Maechler maechler at stat.math.ethz.ch
Tue Aug 19 15:02:51 CEST 2003

>>>>> "ThomasB" == Thomas W Blackwell <tblackw at umich.edu>
>>>>>     on Tue, 19 Aug 2003 08:14:45 -0400 (EDT) writes:

    ThomasB> On Wed, 20 Aug 2003, Zitan Broth wrote:
    ThomasB> . . .

    >> What I am trying to do is use R as part of a web-based system and call R
    >> from PHP.  The common method of interfacing from PHP to many systems is via
    >> the command line (although I could use swig to access R directly but that is
    >> phase 2 ;-) ).  I found in the install notes that I could call
    >> Rterm.exe --no-restore --no-save < infile > outfile (windows, although I
    >> will be rolling out to *nix) however I cannot find a reference of how to
    >> call r-functions from the command line with this -- or perhaps I've missed
    >> the point ?

    ThomasB> infile is an ascii file of R commands - the same
    ThomasB> commands and same syntax which you would type in
    ThomasB> the R command line window.  Frequently the first
    ThomasB> line sets some session options using options(), the
    ThomasB> second line reads some data from a separate, named
    ThomasB> file into an R object, and the third and subsequent
    ThomasB> lines operate on that data and print out the
    ThomasB> results.

    ThomasB> For example:

    ThomasB> options(digits=4, width=88, length=1e+8)
    ThomasB> object <- read.table("data.file")
    ThomasB> summary(lm(y ~ 1 + a + b, object)

    ThomasB> (This assumes that "data.file" contains columns
    ThomasB> named a, b, y in any order.  It does a linear
    ThomasB> regression and prints out the results .. in the
    ThomasB> command line window if you were working
    ThomasB> interactively, but to outfile if R is running
    ThomasB> noninteractively with the call above.)

Thanks a lot, Thomas, for the nice explanation..

    ThomasB> (Gosh, my recollection is that in unix the call is
    ThomasB> R BATCH infile outfile, but I could be mistaken.
    ThomasB> That's on the unix man page for R if you forget.)

Both work in Unix (after replacing "Rterm.exe" by "R"). 
Even R BATCH infile does.
The "--no-restore --no-save" in all versions, since the S-back
compatible way is to work with `persistent' objects (via an .RData file).
This persistence if often undesired when working with scripts,
and I'd recommend the above switches and using "save(.. , file=) and
load(file=..)" if desired, with carefully chosen file names

    >> I did find in the FAQ: 7.22 How can I get command line
    >> editing to work?  But I'm not sure I understand the
    >> answer ..

    ThomasB> If running R non-interactively, you don't care.

    >> So say as a simple example I want to call sd() (standard deviation) from the
    >> command line what would I type ... or do I need to write some R code and
    >> call this .. ?

    ThomasB> Need code to read in the data, then a one line
    ThomasB> command sd(object).  The returned value is printed
    ThomasB> automatically if it is not assigned.

    >> I will continue to read .... Z.

    ThomasB> HTH  -  tom blackwell  -  u michigan medical school  -  ann arbor  -
Martin Maechler

More information about the R-help mailing list