R(1) | User Commands | R(1) |
r - Provides hash-bang (#!) capability for R (www.r-project.org)
r [options] [-|file] [R arguments]
Launches GNU R to execute the specified file containing R commands, or takes commands from stdin if '-' is used to denote stdin, using the specified options. This makes it suitable to create R scripts via the so-called shebang '#!/' line. The optional R arguments are stored in the R vector argv.
r can be used in command-line 'pipes' as well as in 'shebang'-style scripts.
Piping R commands:
echo 'cat(pi^2,"\n")' | r
Equivalently, R commands can be given on the command-line:
r -e 'cat(pi^2, "\n")'
Alternatively, commands can be stored in a file, which in turn might use R command 'readLines' to process stdin input:
$ cat examples/fsizes.r
fsizes <- as.integer(readLines())
print(summary(fsizes))
stem(fsizes)
which can be evaluated by r with the following command:
ls -l /boot | awk '!/^total/ {print $5}' | r examples/fsizes.r
The script file may contain a "shebang" line:
$ cat examples/install.r
#!/usr/bin/env r
# a simple example to install one or more packages
if (is.null(argv) | length(argv)<1) {
cat("Usage: installr.r pkg1 [pkg2 pkg3 ...]0)
q()
}
repos <- "http://cran.us.r-project.org"
lib.loc <- "/usr/local/lib/R/site-library"
install.packages(argv, lib.loc, repos, dependencies=TRUE)
and if it is executable, it can be called as:
examples/install.r "TeachingDemos"
See the examples directory in the sources for more.
The executable program is called r, but the project is called littler to avoid confusion with the real GNU R.
The GNU R language is documented extensively at the R website (http://www.r-project.org) and in several manuals available in html, info and pdf.
Jeffrey Horner <jeff.horner@vanderbilt.edu>. Dirk Eddelbuettel <edd@debian.org>.
September 2006 | r |