Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run R script from command line

Tags:

command-line

r

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?

like image 954
Sait Avatar asked Aug 19 '13 04:08

Sait


People also ask

How do I run an Rscript from the command line?

The most convenient way to run R scripts from the command line is to use Rscript, an alternative front-end to run R code. Rscript is capable of executing R code from different command interpreters, such as a bash script on Linux or a Task Scheduler task on Windows.

Can you use R in command line?

You can now interact with R using the command line interface. As you develop your skills using R, you may want to save your work for a later session or run a block of code to initialize your session environment.

How do I run an R file in Windows command line?

After you add Rscript to the path, in my case by adding the folder C:\Program Files\R\R-4.0. 2\bin\x64 to the path, be sure to open and close CMD for PATH to refresh. Then open a new window of CMD, and type Rscript , and tell me your output.

How do I run an Rscript in bash?

Use Rscript to run R from bash You can run it using Rscript test. r . And even better, if you add an initial shebang line #!/usr/bin/env Rscript in the script above and make it executable with chmod +x test. r , you can directly launch your R script with ./test.


3 Answers

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.

like image 118
Dason Avatar answered Oct 22 '22 02:10

Dason


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")' 
    
like image 44
biocyberman Avatar answered Oct 22 '22 03:10

biocyberman


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

like image 23
B.Kocis Avatar answered Oct 22 '22 04:10

B.Kocis