643

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?

epo3
  • 2,991
  • 2
  • 33
  • 60
Sait
  • 19,045
  • 18
  • 72
  • 99

7 Answers7

850

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.

Dason
  • 60,663
  • 9
  • 131
  • 148
  • 34
    Without the #! your command line tries to run it as a command-line script, using the same interpreter that interprets your commands. It doesn't know its supposed to be R, even if the file ends in a .R or .r suffix. The #! tells the command line what language is contained in the file. – Spacedman Aug 19 '13 at 06:54
164

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")' 
    
KiriSakow
  • 957
  • 1
  • 12
  • 22
biocyberman
  • 5,675
  • 8
  • 38
  • 50
  • 18
    To run a command you could also use `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 08:55
  • 1
    Exactly what I was looking for. Thank you! – Dominique Paul Jul 07 '21 at 07:36
  • 1
    I am installing a local library which requires some options to be input by user. Is there a way I can pass in a default value for those options? Or just skip them? e.g it asks me "If you want to install location features, press 1, else 2". – Mazhar Ali Jan 25 '23 at 11:08
63

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)?.

nbro
  • 15,395
  • 32
  • 113
  • 196
B.Kocis
  • 1,954
  • 20
  • 19
26

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()
thelatemail
  • 91,185
  • 12
  • 128
  • 188
Mehul Rathod
  • 1,244
  • 8
  • 7
14

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")'
Shicheng Guo
  • 1,233
  • 16
  • 19
  • 5
    Note that it will be simpler to skip loading the library; `R -e 'markdown::rpubsUpload("normalDev","NormalDevconJuly.html")'` – gregmacfarlane Aug 08 '16 at 18:33
6

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
2

Save the code in ABC.R, the file looks like this:

sayHello <- function(){
   print('hello')
}

sayHello()

Create another file called run.cmd and place the following line there:

"C:\Install\R\Rscript.exe" "C:\Users\ABC.R"

Double click on run.cmd and it runs your R code.

Eldorado
  • 69
  • 4