Find file
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (45 sloc) 1.21 KB
title header-includes output

Learning Objectives:

  • Invoking and running R scripts with redirection
  • Execute some scripts


Another way to execute R scripts in the command line is with the R command and the redirection operator <.

Consider the file myscript1.R in the scripts/ directory. This file generates two random vectors, fitting a regression line, and generates two plots:

# Regression analysis of two random vectors

# random data
x <- rnorm(20)
y <- x + rnorm(20)

# regression line
reg <- lm(y ~ x)

# scatter diagram with fitted regression line
plot(x, y, las = 1, pch = 19, col = "#555555")
abline(reg, col = "#0000DD59", lwd = 2)

# residuals plot
plot(x, reg$residuals, las = 1, pch = 19, col = "#606060")
abline(h = 0)

Here's how to execute this script in the command line with a redirection operator:

R --vanilla < myscript1.R

Passing arguments

You can also pass arguments with the --args options of the R command:

R --vanilla --args 10 5 2 < normal-vector.R