R, a powerful statistical programming language, provides a versatile environment for data analysis, visualization, and statistical modeling. While many users interact with R through graphical interfaces, understanding how to invoke R from the command line offers a more flexible and efficient workflow. This guide will explore the various options and considerations for invoking R from the command line on UNIX, Windows, and macOS.
Invoking R from the Command Line
Basic Syntax
The fundamental command to start the main R program from the command line is:
R [options] [<infile] [>outfile]
Additionally, R can be invoked via the `R CMD` interface, acting as a wrapper for various R tools. This is particularly useful for tasks like processing files in R documentation format or manipulating add-on packages.
Platform-Specific Considerations
- On Windows, it is recommended to use `Rterm.exe` at the command line.
- Ensure that the environment variable `TMPDIR` is either unset or points to a valid location for creating temporary files and directories.
Startup Mechanism
R follows a specific startup sequence when invoked from the command line. Notable steps include:
1. Environment Variable Processing:
- R searches for user and site files to set environment variables.
- Site file is determined by the `R_ENVIRON` variable or defaults to `R_HOME/etc/Renviron.site`.
- User file is determined by `R_ENVIRON_USER` or defaults to `.Renviron` in the current or user's home directory.
2. Startup Profiles:
- Site-wide startup profile is searched unless `--no-site-file` is specified.
- User profile is searched unless `--no-init-file` is specified.
- A saved workspace from `.RData` is loaded unless `--no-restore` or `--no-restore-data` is specified.
3. Function Execution:
- If a function `.First()` exists, it is executed at the beginning of the R session.
Memory Control Options
Users can control the memory available to the R process using options detailed in the online help for the 'Memory' topic. These options are typically not needed unless there's a requirement to limit the memory used by R.
Command-Line Options
R accepts various command-line options to customize its behavior. Some notable options include:
- `--help` or `-h`: Print a short help message and exit.
- `--version`: Print version information and exit.
- `--encoding=enc`: Specify the encoding for input from the console or stdin.
- `--save` and `--no-save`: Control whether data sets should be saved at the end of the R session.
- `--vanilla`: Combine multiple options for a minimal startup, useful for non-interactive sessions.
R CMD Interface
The `R CMD` interface allows the invocation of various tools useful in conjunction with R. Some key tools include:
- BATCH: Run R in batch mode with specified options.
- COMPILE: Compile C, C++, Fortran files for use with R.
- INSTALL and REMOVE: Manage add-on packages.
- Sweave: Process Sweave or other vignette documentation.
- Rd2pdf: Convert Rd format to PDF.
Users can obtain usage information for each tool by appending `--help` to the command.