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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With