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?
If you want the output to print to the terminal it is best to use Rscript
Rscript a.R
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.
R CMD BATCH a.R
# Check the output
cat a.Rout
If you really want to use the ./a.R
way of calling the script you could add an appropriate #!
to the top of the script
#!/usr/bin/env Rscript
sayHello <- function(){
print('hello')
}
sayHello()
I 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.
R CMD BATCH
is terrible. Anything but that ...
– Dirk Eddelbuettel
Jan 18 '15 at 2:27
This does not answer the question directly. But someone may end up here because they want to run a oneliner of R from the terminal. For example, if you just want to install some missing packages and quit, this oneliner can be very convenient. I use it a lot when I suddenly find out that I miss some packages, and I want to install them to where I want.
R -e 'install.packages(c("package1", "package2"))' # install to default location.
sudo R -e 'install.packages(c("package1", "package2"), lib="/usr/local/lib/R/site-library")' # install to location that requires root.
Rscript -e "getwd()"
in the terminal. Rscript will only print the command output and not the full R startup message.
– Paul Rougieux
Dec 30 '15 at 8:55
r -e "cat(getwd(),'\n')"
if you have littler installed. In this answer Dirk Eddelbuettel explains the difference between littler and Rscript.
– Paul Rougieux
Dec 30 '15 at 9:15
R -e 'install.packages("package", repos="http://cran.us.r-project.org")'
– Erin
Oct 18 '16 at 6:22
R -r 'options(warn=2); install...'
in order to halt the execution and get a non-zero error code in case the installation fails. Otherwise, any install.packages
errors are just warnings.
– rvernica
Jan 18 '17 at 16:41
One more way of running an R script from the command line would be:
R < scriptName.R --no-save
or with --save
.
See also What's the best way to use R scripts on the command line (terminal)?.
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
## example #! script for a Unix-alike
#! /path/to/Rscript --vanilla --default-packages=utils
args <- commandArgs(TRUE)
res <- try(install.packages(args))
if(inherits(res, "try-error")) q(status=1) else q()
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
R -e 'library("rmarkdown");library("knitr");rmarkdown::render("NormalDevconJuly.Rmd")'
R -e 'library("markdown");rpubsUpload("normalDev","NormalDevconJuly.html")'
R -e 'markdown::rpubsUpload("normalDev","NormalDevconJuly.html")'
– gregmacfarlane
Aug 8 '16 at 18:33
Just for documentation. Sometines you need to run the scrip as sudo
:
sudo Rscript path/to/your/file.R
Yet another way to use Rscript for *Unix systems is Process Substitution.
Rscript <(zcat a.r)
# [1] "hello"
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.:
Rscript <(sed s/hello/bye/ a.r)
# [1] "bye"
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.:
Rscript <(echo "head(iris,2)")
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 5.1 3.5 1.4 0.2 setosa
# 2 4.9 3.0 1.4 0.2 setosa
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
#!/usr/bin/env Rscript
– michael Jun 25 '16 at 21:39