BATCH, R programs which illustrate how an R program can be executed in batch mode.
In particular, a file might be created, called "example1.r", and containing the commands:
x <- c(1,2,3,4,5,6) # Create ordered collection (vector) y <- x^2 # Square the elements of x print(y) # print (vector) y mean(y) # Calculate average (arithmetic mean) of (vector) y; result is scalar var(y) # Calculate sample variance lm_1 <- lm(y ~ x) # Fit a linear regression model "y = f(x)" or "y = B0 + (B1 * x)" # store the results as lm_1 print(lm_1) # Print the model from the (linear model object) lm_1 summary(lm_1) # Compute and print statistics for the fit of the (linear model object) lm_1 par(mfrow=c(2, 2)) # Request 2x2 plot layout q() # quit
On a UNIX system, the R program could be invoked to carry out these commands, using a UNIX shell script that might read:
R --vanilla < example1.r > example1_output.txt
The computer code and data files on this web page are distributed under the MIT license
EXAMPLE3 requires that the library caTools be installed.