0

I am a engineer newbie to R and have got a script which I have to run on linux, I searched alot but was not able to find a simple command to run the script on linux. I have to couple my engineering software with R so I need to run it also on linux.

My script name is  myscipt.R and I want to use 2 cpus to run that script.

kindly help me with the startup.

best regards.

hamad khan
  • 369
  • 1
  • 5
  • 14

1 Answers1

2

This works for me:

R CMD BATCH myscript.R

As an example, the following should produce a .csv file of a random generated matrix. I have always named this scripts with an ".R" extension, but I'm not sure that that is necessary.

The script to call (named "testscript.R") contains the following lines of code:

set.seed(1)
M<-matrix(runif(20),5,4)
write.csv(M, file="M.csv")

Then call this script in your console as illustrated above:

R CMD BATCH testscript.R

On my machine, this produces the "M.csv" document, which looks like this:

    "","V1","V2","V3","V4"
"1",0.2655086631421,0.898389684967697,0.205974574899301,0.497699242085218
"2",0.37212389963679,0.944675268605351,0.176556752528995,0.717618508264422
"3",0.572853363351896,0.660797792486846,0.687022846657783,0.991906094830483
"4",0.908207789994776,0.62911404389888,0.384103718213737,0.380035179434344
"5",0.201681931037456,0.0617862704675645,0.769841419998556,0.777445221319795

Also, a "testscript.Rout" file is produced giving the R console output:

R version 2.14.0 (2011-10-31)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: i386-pc-solaris2.10 (32-bit)

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 and
'citation()' on how to cite R or R packages in publications.

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

[Previously saved workspace restored]

> set.seed(1)
> M<-matrix(runif(20),5,4)
> write.csv(M, file="M.csv")
> 
> proc.time()
   user  system elapsed 
  9.075   0.257   9.362 

Hope that explains it better.

Marc in the box
  • 11,769
  • 4
  • 47
  • 97
  • 1
    Or simply: `Rscript myscript.R`. – Vincent Zoonekynd Jun 06 '12 at 09:06
  • unfortunately both the commands doesnt work and the server doesnt recogniye the commands R or Rscript. what could be the problem? – hamad khan Jun 12 '12 at 12:04
  • @hamad khan - are you able to open R by entering "R" in your console command line? – Marc in the box Jun 12 '12 at 12:44
  • @Marcinthebox I am using it on linux server.. I enter the command it genrate two more files.. its .Rdata and .Rout files but not running it.. so what could be the problem?.. i think its not running properly , may be i am novice.. – hamad khan Jun 12 '12 at 14:18
  • 1
    @hamad khan - I hope the more elaborated answer (above) helps you. I'm not sure exactly why its not working for you. Maybe the .Rdata object is your output? By looking in the .Rout object, you should see if there are any parts of the script having problems. In any case, try the above example and see if you are also able to produce the M.csv file. – Marc in the box Jun 13 '12 at 08:36