rscript
Execute R scripts from the command line
TLDR
Run a script
Run a script in vanilla mode (i.e. a blank session that doesn't save the workspace at the end)
Execute one or more R expressions
Display R version
SYNOPSIS
Rscript [options] file [args]
Rscript [options] -e expression [args]
PARAMETERS
-e expression
Executes the R code provided directly as expression. This is useful for running short R commands without creating a script file.
--vanilla
Starts R without saving or restoring the workspace, history, or startup files (like .Rprofile). This is the default behavior for rscript and is recommended for clean, reproducible runs.
--save
Explicitly saves the workspace image to .RData in the current working directory at the end of the script execution.
--no-save
Prevents saving the workspace image. This is the default for rscript, ensuring a clean exit without modifying the environment.
--restore
Explicitly restores a previously saved workspace image from .RData. This is not the default for rscript.
--no-restore
Prevents restoring a previously saved workspace. This is the default for rscript.
--slave
A convenient shorthand for --vanilla --no-restore --no-save --no-environ --no-site-file --no-Renviron --no-Rprofile --no-readline. Ensures a very clean, quiet, and non-interactive start, ideal for background processes.
--verbose
Prints detailed messages during the R startup process, which can be helpful for debugging environment issues.
--version
Prints the R version information (including platform, R version, etc.) and then exits immediately.
--help
Displays a help message with available options for rscript and then exits.
--args
Signals that all subsequent arguments on the command line should be passed directly to the R script itself, rather than being interpreted as rscript options. These can be accessed via commandArgs(trailingOnly=TRUE) within R.
--default-packages=list
Specifies a comma-separated list of packages to be loaded automatically at R startup, overriding the default set of packages.
DESCRIPTION
rscript is a command-line utility that allows for the execution of R scripts or R expressions directly from the shell, bypassing the need for an interactive R session. It is an essential tool for automating R-based tasks, batch processing data, and integrating R code into larger shell scripts or workflows. By default, rscript operates in a non-interactive mode, meaning it does not save or restore the workspace, ensuring clean and reproducible script executions. It can execute code from a specified R script file or directly from a string expression provided as an argument, making it highly flexible for various scripting needs. Its primary purpose is to enable R to be used seamlessly in automated environments.
CAVEATS
By default, rscript operates in a clean, non-interactive mode, meaning it does not save or restore the workspace (similar to --vanilla). This is highly beneficial for reproducibility and automation but means any persistent data or objects must be explicitly handled within your R script (e.g., by reading/writing files). Since there's no interactive graphical display, any plots or visualizations must be saved to a file format (e.g., PDF, PNG) directly from within the R script. Robust error handling and logging should be managed either within the R script or by monitoring rscript's exit code in the calling shell script.
ARGUMENT PASSING TO R SCRIPTS
Arguments provided on the command line after the script filename (or -e expression) are accessible within the R script using the commandArgs(trailingOnly=TRUE) function. For example, if you run rscript myscript.R arg1 arg2, then commandArgs(trailingOnly=TRUE) within myscript.R would return a character vector c("arg1", "arg2").
EXIT STATUS
rscript typically exits with status 0 on successful execution. If the R script itself explicitly calls quit(status=N), where N is a non-zero integer, rscript will propagate that exit status. This feature is crucial for robust error checking in shell scripts and automated workflows, allowing them to react appropriately to failures within the R script.
HISTORY
rscript was introduced as a streamlined front-end for executing R code non-interactively, making it significantly easier to integrate R scripts into shell environments, cron jobs, and other automated systems. Prior to its existence, users often relied on invoking the main R executable with specific batch-mode arguments (e.g., R CMD BATCH). rscript simplifies and standardizes these common non-interactive use cases, providing a more intuitive interface for scripting.
SEE ALSO
R(1), R CMD BATCH(1), sh(1)