HOWTO · R
How to Run an R Script From the Command Line
Learn how to run an R script from a terminal or command prompt with Rscript, pass arguments, schedule a cron job, and track scripts with Git.
On this page
To run an R script from a terminal or command prompt, save the code in a file and invoke it with Rscript path/to/script.R. R must be installed, and your shell must be able to find Rscript through PATH; otherwise, use the executable’s full installation path. This workflow is useful for repeatable analysis, scripts that accept inputs, and scheduled jobs.
What is Rscript?
Rscript is the command-line program for running R code from a script or a command supplied directly to it. It is suited to non-interactive work: the instructions are in a file or command, and the process ends when that work finishes. An interactive R session, by comparison, lets you enter commands and inspect results as you go.
The R installation includes Rscript. Open a terminal on macOS or Linux, or Command Prompt or PowerShell on Windows, and check whether the command is available with Rscript --version. If the shell reports that it cannot find the command, install R or locate the executable installed with R and use its full path. Installation directories vary by operating system and installation, so avoid relying on a version-specific path copied from another computer.
Running an R Script Using Rscript
Create a file named mean.R with this small example. It uses supplied command-line numbers when present and otherwise uses a built-in set, so the file can be run as shown or adapted to other values.
args <- commandArgs(trailingOnly = TRUE)
numbers <- if (length(args) > 0) as.numeric(args) else c(1, 2, 3, 4, 5)
cat("Mean: ", mean(numbers), "\n", sep = "")
With the default input, the code uses the numbers 1 through 5; by inspection, it is expected to print Mean: 3. This example has not been runtime-verified.
Run it from the directory containing the file with Rscript mean.R. To run it from another directory, give the script path, such as Rscript /path/to/mean.R. On Windows, quote a path containing spaces, for example Rscript "C:/Users/Ada/My Scripts/mean.R"; use the path format accepted by the shell you opened. If Rscript is not on PATH, replace it with the full path to the executable and keep the script path as a separate argument.
For a short expression that does not need a saved file, use the -e option in a POSIX-like shell or PowerShell, for example Rscript -e 'mean(iris$Sepal.Length)'. This single-quoted form is not for Windows Command Prompt, which treats single quotes as literal characters; in Command Prompt, use the saved-script example above. The iris dataset is available in a standard R installation. The --help and --version options request command help and version information; --verbose asks for more verbose command output. These are useful when checking invocation details or diagnosing a run. -e begins with one hyphen, so not every option starts with two.
Passing Arguments to an R Script
A script can read values after its filename with commandArgs(trailingOnly = TRUE). The example above converts each supplied argument to a number and calculates their mean. For example, Rscript mean.R 10 20 30 passes three separate values. If an input itself contains spaces, quote that input in the shell so it arrives as one argument. Keep the script’s expected input format clear, and adapt its conversion and validation when using data other than numbers.
Scheduling R Scripts with Cron Jobs
On Unix-like systems, cron can start a script on a schedule. Edit your user’s crontab with crontab -e, then add an entry such as 0 2 * * * /usr/bin/Rscript /path/to/report.R to request a daily run at 2:00 AM, according to the machine’s cron configuration. The executable path and script path are examples; use the paths on the computer that will run the job.
Scheduled jobs may start with a different working directory and environment than your interactive shell. Use absolute paths for the executable, script, and input or output files, and arrange for the job’s output or errors to be captured if you need to diagnose scheduled runs. Cron is a Unix-like scheduling option; Windows users can use the system’s task scheduler instead.
Integrating R with Git for Version Control
Git can track changes to R scripts and related project files, making it easier to review edits or restore an earlier version. It is optional: Git is not needed to run a script with Rscript. In a project directory, initialize a repository with git init, add the files you want to track with git add, and record a snapshot with git commit. Review what will be committed, and avoid adding generated data or credentials unless they belong in the project history.
Conclusion
Save your code in an R file, then run it with Rscript and a script path. Check that R is installed and the executable is available to your shell, quote paths when needed, and pass input values after the filename. For recurring work, use a scheduler with paths suited to its environment; use Git when you want to track script changes.
FAQ
-
How do I install R and Rscript?
Install R for your operating system using its installation instructions.Rscriptis included with R; there is no separate Rscript installation in this workflow. -
Can I run an R script on Windows?
Yes. Use Command Prompt or PowerShell and runRscript script.Rif the executable is available onPATH. Otherwise, invoke the installed executable by its full path. Quote paths that contain spaces according to the shell’s quoting rules. -
What is the difference between Rscript and R?
Rscriptis intended for running a script or expression from a command line. TheRcommand commonly starts an interactive session, where you can enter commands and inspect results as you work. -
How can I check the installed Rscript version?
RunRscript --versionin your terminal or command prompt. The command must be available onPATH, or invoked by its full path. -
How can I run multiple R scripts one after another?
In a Unix-like shell, join commands with&&, such asRscript first.R && Rscript second.R. The second command runs only if the first succeeds. For a longer sequence, put the invocations in a shell script or another scheduler appropriate to your system.