← Back to R Course | Chapter 1: Getting Started | Lesson 3 of 6

Running Scripts

An R script is a saved list of commands you can run all at once, like a recipe you follow from top to bottom. You run it from the terminal with a single command.

In this page:

  1. Running Scripts
Syntax
r
Rscript script_name.R

Running Scripts

An R script is run from the command line with Rscript filename.R, executing every statement top to bottom. This is the standard way to run non-interactive R code, such as in automation or batch jobs.

The interactive console and Rscript behave almost identically except for automatic printing.

Note:
  • Use Rscript for batch execution.
  • Use the interactive R console (or RStudio) for exploratory work.

Example: Running Scripts

bash
# Run an R script from the command line
Rscript analysis.R

# Start the interactive R console
R

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Running Rscript analysis.R from the wrong folder, so R reports cannot open file 'analysis.R'.
  2. Expecting values to print automatically in a script like the console does, when a script needs print() or cat() for most results.
  3. Typing Rscript in the R console instead of the command line, where it is not an R function.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.