Run R Script from Command Line

Run R script from 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.

Running R Code from Command Line (Windows)

  1. You want Rscript.exe.

  2. You can control the output from within the script -- see sink() and its documentation.

  3. You can access command-arguments via commandArgs().

  4. You can control command-line arguments more finely via the getopt and optparse packages.

  5. If everything else fails, consider reading the manuals or contributed documentation

How do I run an R script from within RStudio's built-in R console?

Short Answer using source() function

Once you download, install, and open the RStudio, you'll see a part in the lower left with blue greater than symbols >.

In the part of RStudio's GUI with the blue >, enter the following

> setwd('/folder/where/the/file/is/')
> source('file_name')`
...output, if any, appears below...

Example:

Let's assume I have a file at /home/myusername/prj/r/learn_r/insurance_data.r that I want to run.

I would start up RStudio, and enter the following in the little window it has labeled Console:

Remember, it's setwd, not setpwd

Remember the quotation marks! Don't be silly like me.


Annoyingly long answer with screenshots using source() function

Well, it turned out to be much simpler than I expected to run this from RStudio's built-in console. I was surprised that this had not already been asked about RStudio, before. If it has, I guess I'll have a burned question.

Anyway, a little trial and error showed me how to do this:

Sample Image

Yay, output has appeared below.

Make sure to set your working directory, first.

I did this as follows from inside RStudio 1.0.143 on my Ubuntu 16.04 LTS environment:

setwd("~/proj/r/learn_r")

Sample Image

Next, you can enter help(source), you can search for the syntax of the source() function, and you can just type it in to the RStudio console for a prompt:

Sample Image

How to call R script from command line with multiple augument types (inc. list)

@MrFlick deserves credit for this answer. The issue was I was not accounting for a situation where the number of arguments would be greater than 7 (duh).

A quick very fix:

if (length(args) < 7) 
{
stop('At least seven arguments must be supplied.', call.=FALSE)
}

if (length(args)==7)
{
project = args[1]
method = args[2]
lib_path = args[3]
storage = args[4]
compo = args[5]
res_file = args[6]
integrated_object = args[7]
}

if (length(args)>7)
{
project = args[1]
method = args[2]
lib_path = args[3]
storage = args[4]
compo = args[5]
res_file = args[6]
integrated_object = args[7:length(args)]
}

Thank you for your eyes @MrFlick

How to run R script without opening R or RStudio?

There are mainly two ways. with using Rscript, like below:

C:\Users\automat7> Rscript app.r

or in some cases, like with shiny or when running a one line script, usually, you can use

R -e "shiny::runApp(address_to_folder, args)"

You may need to add the R's bin folder to your PATH environment variable if you are using Windows.
You can follow the instructions here for that: How to Add a folder to Path environment variable in Windows10



Related Topics



Leave a reply



Submit