I have a file, called a.r
, it has a chmod
of 755,
sayHello <- function(){
print('hello')
}
sayHello()
How can I run this via command-line?
I have a file, called a.r
, it has a chmod
of 755,
sayHello <- function(){
print('hello')
}
sayHello()
How can I run this via command-line?
Yet another way to use Rscript for *Unix systems is Process Substitution.
Which obviously does the same as the accepted answer, but this allows you to manipulate and run your file without saving it the power of the command line, e.g.:
Similar to
Rscript -e "Rcode"
it also allows to run without saving into a file. So it could be used in conjunction with scripts that generate R-code, e.g.:Just for documentation. Sometines you need to run the scrip as
sudo
:You need the
?Rscript
command to run an R script from the terminal.Check out http://stat.ethz.ch/R-manual/R-devel/library/utils/html/Rscript.html
Example
One more way of running an R script from the command line would be:
or with
--save
.See also What's the best way to use R scripts on the command line (terminal)?.
How to run Rmd in command with knitr and rmarkdown by multiple commands and then Upload an HTML file to RPubs
Here is a example: load two libraries and run a R command
If you want the output to print to the terminal it is best to use Rscript
Note that when using
R CMD BATCH a.R
that instead of redirecting output to standard out and displaying on the terminal a new file called a.Rout will be created.If you really want to use the
./a.R
way of calling the script you could add an appropriate#!
to the top of the scriptI will also note that if you're running on a *unix system there is the useful littler package which provides easy command line piping to R.