Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running r scripts or commands with interpretor in unix for unix-layman

Tags:

linux

shell

unix

r

I am layman to unix and sofar I using R in windows. For example I type following in my R session (in R gui).

# this is a my funny example script 
X <- 1:10
Y <- 21:30
plot(X, Y)
myfun <- function (x){
              x1 <- x^0.2
              return (x1)
             }
myfun(X)

How can I achieve this in unix shell, in two situations -

(1) directly in command line via an interpeter (2) creating a script and running script.

Please provide step considering I am layman to unix.

like image 441
SHRram Avatar asked May 15 '12 14:05

SHRram


People also ask

How do I run a command in Rscript?

To run an R command, put the cursor on the line of the command and then click the Run button at the top of the file window. Or just press CTRL-Enter.


2 Answers

Assuming you save your script in a simple text file with the name so.R, you can run it under Linux/Unix by typing R at the prompt. Once in R enter

  source('so.R')

to execute the script inside the R environment (this assumes the so.R file is in the same directory as you are when you issue this command).

To run the script from the Linux/Unix command line use the following command:

  R CMD BATCH so.R

Note that I got the plot to show when I ran the script inside of R, but from the Linux command line it doesn't show. I suspect it gets quickly displayed and then goes away, so there will be a R command that you have to look up to make it pause after it displays the plot.

like image 162
Levon Avatar answered Oct 12 '22 10:10

Levon


If your program is going to work on a single dataset, then simple-r might be the solution:

http://code.google.com/p/simple-r/

It is especially designed for simple statistical analysis as a part of Linux command line. For example, if one wants to plot some data, 'r -p data.txt' will do the job; for getting correlation coefficient: 'r cor data.txt' will suffice.

like image 26
Tom Avatar answered Oct 12 '22 09:10

Tom