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?
Join Stack Overflow to learn, share knowledge, and build your career.
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
One other thing to note about using Rscript is that it doesn't load the methods
package by default which can cause confusion. So if you're relying on anything that methods provides you'll want to load it explicitly in your script.
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. It may be necessary to use littler to run shiny apps via a script? Further details can be found in this question.
R CMD BATCH
is terrible. Anything but that ...
– Dirk Eddelbuettel
Jan 18 '15 at 2:27
R CMD INSTALL -l ~/R/lib-dev
– mikemtnbikes
Mar 26 '19 at 17:07
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.
To install to the default location:
R -e 'install.packages(c("package1", "package2"))'
To install to a location that requires root
privileges:
R -e 'install.packages(c("package1", "package2"), lib="/usr/local/lib/R/site-library")'
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
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
Just for documentation, sometimes you need to run the script as sudo
:
sudo Rscript path/to/your/file.R
#!/usr/bin/env Rscript
– michael Jun 25 '16 at 21:39