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:
Syntax
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
Rscriptfor batch execution. - Use the interactive R console (or RStudio) for exploratory work.
Example: Running Scripts
# 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
- Running
Rscript analysis.Rfrom the wrong folder, so R reportscannot open file 'analysis.R'. - Expecting values to print automatically in a script like the console does, when a script needs
print()orcat()for most results. - Typing
Rscriptin 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: