[R] Writing a R-Script

(Ted Harding) Ted.Harding at manchester.ac.uk
Thu Oct 18 14:45:33 CEST 2007


On 18-Oct-07 11:56:48, erkan yanar wrote:
> 
> Ist there a possibility to write a R-Script using something like
> 
>#!/usr/bin/R
> and then alle the requestet commands?
> 
> Of course "R CMD BATCH" exists, but there was (for me) no
> possibility to write something like a HERE-script.
> regards
> erkan

Hmmm -- not that I've tried it before, but here is a copy
of a brief R session I just ran from the keyboard (lines
1-5 typed without the initial "$ " and "> " of course):

$ R --no-save << EOT
> x<-2*pi*(0.1*(0:10))
> print(cbind(x,sin(x)))
> quit("no")
> EOT

R : Copyright 2003, The R Development Core Team
Version 1.8.0  (2003-10-08)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for a HTML browser interface to help.
Type 'q()' to quit R.

> x<-2*pi*(0.1*(0:10))
> print(cbind(x,sin(x)))
              x              
 [1,] 0.0000000  0.000000e+00
 [2,] 0.6283185  5.877853e-01
 [3,] 1.2566371  9.510565e-01
 [4,] 1.8849556  9.510565e-01
 [5,] 2.5132741  5.877853e-01
 [6,] 3.1415927  1.224606e-16
 [7,] 3.7699112 -5.877853e-01
 [8,] 4.3982297 -9.510565e-01
 [9,] 5.0265482 -9.510565e-01
[10,] 5.6548668 -5.877853e-01
[11,] 6.2831853 -2.449213e-16
> quit("no")
$

So that's R running a pure "HERE document".

This also worked when I ran a file "Rbatch" containing

#! /bin/bash
/usr/bin/R --no-save << EOT
x<-2*pi*(0.1*(0:10))
print(cbind(x,sin(x)))
quit("no")
EOT


(don't forget to chmod 755 the Rbatch file!)

For some reason, however, an alternative form of Rbatch:

#!/usr/bin/R --no-save
x<-2*pi*(0.1*(0:10))
print(cbind(x,sin(x)))
quit("no")


does not work:
$ ./Rbatch
./Rbatch: line 2: syntax error near unexpected token `('
./Rbatch: line 2: `x<-2*pi*(0.1*(0:10))'

indicating that 'bash' itself is trying to interpret the
R commands, despite what is said in 'man bash':

   If the program is a file beginning with #!,
   the remainder of the  firstline  specifies
   an interpreter for the program.

Hmm ...
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 18-Oct-07                                       Time: 13:45:30
------------------------------ XFMail ------------------------------



More information about the R-help mailing list