R, a powerful statistical computing language, is widely used for data analysis, visualization, and statistical modeling. While commonly associated with UNIX-based systems, R is also well-supported on Windows. In this guide, we'll explore the various methods to invoke R under Windows, including the use of terminal commands and the graphical user interface (GUI).
Running R from the Terminal
Using R.exe and Rterm.exe
When working within a terminal window such as cmd.exe, you can run R using either `R.exe` or `Rterm.exe`. These executables provide different levels of interaction. For interactive use, `Rgui.exe` provides a console-based GUI.
Clarifying Home Directory
The startup procedure on Windows closely resembles that on UNIX, but with some nuances. The 'home directory' on Windows is determined by the following priorities:
- If the environment variable `R_USER` is defined, it sets the home directory.
- If `HOME` is defined, it takes precedence.
- R attempts to find system-defined home directories, starting with the Windows "personal" directory (e.g., My Documents).
- If unsuccessful, and `HOMEDRIVE` and `HOMEPATH` are defined, they dictate the home directory.
- If all else fails, the home directory is set to the starting directory.
Managing Temporary Files
Ensure that environment variables `TMPDIR`, `TMP`, and `TEMP` are either unset or point to valid locations for creating temporary files and directories.
Workspace Restoration
If an argument ending in `.RData` is provided, it is interpreted as the path to the workspace to be restored. This implies `--restore`, setting the working directory to the parent of the named file. This mechanism is particularly useful for drag-and-drop operations and file associations with `RGui.exe`.
Additional RGui.exe Command-Line Options
When invoking `RGui.exe` from the command line, several options are available:
- `--mdi` or `--sdi`: Control whether Rgui will operate as an MDI (Multiple Document Interface) or an SDI (Single Document Interface) program. This overrides settings in the user's Rconsole file.
- `--no-mdi`: Disable MDI or SDI based on the command-line setting.
- `--debug`: Enable the "Break to debugger" menu item in Rgui, triggering a break to the debugger during command-line processing.
R CMD and Custom Scripts
Under Windows with `R CMD`, you can specify your own `.bat`, `.exe`, `.sh`, or `.pl` file. This file will be run under the appropriate interpreter (e.g., Perl for `.pl`), with several environment variables set appropriately, including `R_HOME`, `R_OSTYPE`, `PATH`, `BSTINPUTS`, and `TEXINPUTS`.
For example:
R CMD latex.exe mydoc
This command runs LaTeX on `mydoc.tex`, appending the path to R’s share/texmf macros to `TEXINPUTS`.