[R] Command line R / PHP?

Thomas W Blackwell tblackw at umich.edu
Tue Aug 19 14:14:45 CEST 2003

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

> 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 ?

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

For example:

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

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

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

> 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 ..

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 .. ?

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

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

HTH  -  tom blackwell  -  u michigan medical school  -  ann arbor  -

More information about the R-help mailing list