Using the R command (r): Quick Start


Overview

The r command starts the R language interpreter. It’s your entry point for interactive work, scripting, and quick one-liners in R.

Quick start: start the REPL

  • Open an interactive R session:
R
  • What you can do next:
    • Type R expressions directly and press Enter to evaluate.
    • Use Ctrl-D (or type q()) to quit.

## Start R in vanilla mode
Vanilla mode gives you a clean R session (no saved workspace) which helps avoid mixing state from previous runs.

R —vanilla

or

R {{[-v|—vanilla]}}


## Run an R script file
If you have a script at path/to/file.R, you can execute it from the shell:

R —file path/to/file.R

R -f path/to/file.R


Notes:
- The script will run in the current environment unless you explicitly modify it.
- Any errors in the script will stop execution unless error handling is present.

## Execute an expression and exit
For quick one-off tasks, run a single expression and quit:

R -e ‘summary(c(1,2,3,4))’

R -e {{expr}}


## Start with a debugger
If you need to debug an R session or a script, you can specify a debugger:

R —debugger gdb path/to/script.R

R {{[-d|—debugger]}} {{debugger}}


Common debugging tip: use print statements or browser() within your script to pause execution and inspect state.

## Check an R package from a source directory
If you’re developing an R package and want to run package checks from source, you can use:

R CMD check /path/to/package_source

R CMD check {{path/to/package_source}}


Tip: Run checks in a clean environment or container to avoid local inconsistencies.

## Display version information
Knowing the R version helps when following tutorials or reporting issues:

R —version

R —version


## Common pitfalls to avoid
- Forgetting to install required packages before running code. Use install.packages("pkgName") inside R or via Rscript.
- Running in vanilla mode but forgetting to install user-specific startup options; vanilla disables .Rprofile and site profiles.
- Assuming relative paths are always correct; use absolute paths or set working directory with setwd("/path/to/dir").
- Mixing Rscript for scripts and R for interactive work; for scripts use Rscript if you don’t need an interactive environment.

## Quick examples you can copy
- Start R and print a vector summary:

R -e ‘summary(c(5, 2, 9, 3))’

- Run a file and print its global environment after execution:

R —file /path/to/script.R


## Summary
- Use R for both interactive work and scripting.
- Vanilla mode helps keep sessions predictable.
- Use R CMD check for package validation.
- Check version to ensure compatibility with tutorials and dependencies.

## See Also
- [Mastering PulseAudio Commands: A Beginners Guide to Managing Your Sound System](/blog/pulseaudio-guide-linux)
- [How to Use the blkid Command in Linux](/blog/blkid-guide-linux)
- [How to Use the iostat Command for Linux Performance Monitoring](/blog/iostat-guide-linux)
- [How to Use the ss Command for Socket Investigation](/blog/ss-guide-linux)
- [How to Use the last Command for User Login Monitoring in Linux?](/blog/last-guide-linux)
- [How to Use the crontab Command for Effective Task Scheduling](/blog/crontab-guide-linux)