R console and scripts
last modified November 20, 2026
An R script does not run in an empty world. The interpreter starts in a
directory, reads a startup file, keeps a workspace of objects, and receives the
arguments that were typed after the file name. The
introduction article showed the first commands;
this article looks at the surroundings that decide where those commands find
their files and their data. It covers the console prompt,
Rscript, the working directory, source, the
.Rprofile startup file, the workspace, and command-line arguments.
The examples use R 4.5.2.
The prompt and the continuation line
Running R in a terminal opens the console, a read-eval-print loop
that waits for one command at a time. The primary prompt is a greater-than
sign. An expression that fits on one line is evaluated when Enter is pressed,
and its value is printed. An assignment stores its value invisibly, so it
prints nothing. When a line is incomplete, for example because an operator is
still waiting for its right side, the prompt changes to a plus sign and the
console reads the rest of the expression.
The session below starts R with the startup banner suppressed and with the
workspace not saved on exit. The first assignment prints nothing, the call to
mean prints its result, and the addition is split over two lines.
The final q() ends the session. The prompts are not part of the
language; they are printed by the console and never appear when a file is run.
$ R --quiet --no-save > scores <- c(72, 85, 90) > mean(scores) [1] 82.33333 > total <- 3 + + 4 > total [1] 7 > q()
Scripts and command-line arguments
Rscript reads a file from top to bottom and exits when the last
expression has been evaluated. The file sees the words typed after its name as
character strings through commandArgs(trailingOnly = TRUE). The
script below prints those arguments and checks whether it is running
interactively. interactive() is FALSE under
Rscript, because there is no console to type into.
args <- commandArgs(trailingOnly = TRUE)
cat("arguments:", args, "\n")
cat("count:", length(args), "\n")
cat("interactive:", interactive(), "\n")
The arguments are stored in a vector called args, and
cat prints all of its elements separated by spaces.
length returns how many words arrived, which is two in this run. The name
args is ordinary here; it is not the args function
from the help system. Without the trailingOnly argument,
commandArgs() also returns the options that belong to R itself,
including the --file= entry that names the script. The command
line below passes two words, and both reach the script unchanged.
$ Rscript arguments.R alpha beta arguments: alpha beta count: 2 interactive: FALSE
The working directory
Every process has a working directory, and every relative path is resolved
against it. getwd returns the absolute path, and
basename shortens it to the last component for printing.
list.files returns the names in the directory.
setwd changes the directory for the rest of the session, and
source reads another R file into the current session instead of
starting a new process, so the objects it creates remain available.
The small file below holds one assignment. A script can reuse it without copying the assignment, which is the usual way to keep data definitions in one place.
notice <- "regional totals ready"
The next script prints its starting directory and its files, moves into the
notes directory, and sources the file from there. Returning with
setwd("..") restores the original directory. The paths are
relative to the directory in which Rscript was started, not to the
location of the script file.
cat("start:", basename(getwd()), "\n")
cat("files:", list.files(), "\n")
setwd("notes")
cat("now:", basename(getwd()), "\n")
source("notes.R")
cat("loaded:", notice, "\n")
setwd("..")
cat("back:", basename(getwd()), "\n")
The output shows the two entries in the project directory, the new working
directory, the value that source created, and the return to the
starting point. If the script is started from another directory, the first
setwd fails, which is why a script that depends on a location
should either use absolute paths or check its starting point first.
$ Rscript paths.R start: path-demo files: notes paths.R now: notes loaded: regional totals ready back: path-demo
Project startup files
Before the first command runs, R reads a user profile from the home directory
and then a project profile named .Rprofile in the working
directory. Both files contain ordinary R code, and both are read by
Rscript as well as by the console. A project profile is a good
place for options that every script in the project should share, and for a few
names that describe the project.
options(digits = 4)
project_name <- "sales"
cat("project profile loaded\n")
The profile sets digits to 4, defines project_name,
and prints one line so that its effect is visible. The script below reads the
option and checks whether the name exists. getOption returns the
current value of an option, and exists reports whether a name is
bound in the current scope.
cat("digits:", getOption("digits"), "\n")
cat("project defined:", exists("project_name"), "\n")
cat("project:", project_name, "\n")
The profile line appears before the script output because the profile is
evaluated first. The digits option is now 4 instead of the default 7, and
project_name is visible to the script. Running the same file with
Rscript --no-init-file skips both profile files: the
exists check reports FALSE, getOption
returns 7, and the last line fails because project_name is no
longer defined. The stronger --vanilla option also ignores
environment files and the saved workspace, which makes it useful for
reproducible batch runs but unsuitable for a script that depends on a project
profile.
$ Rscript startup.R project profile loaded digits: 4 project defined: TRUE project: sales
The workspace
The objects created in a session form the workspace. ls lists
their names in the current environment, rm removes one object, and
rm(list = ls()) removes everything that ls can see.
The list argument takes a character vector of names, which is why
the call is written in that slightly unusual way. Under Rscript
the workspace starts empty and is not saved when the process exits.
draft <- 1
final <- 2
count <- 3
cat("objects:", ls(), "\n")
rm(draft)
cat("after rm:", ls(), "\n")
cat("draft exists:", exists("draft"), "\n")
rm(list = ls())
cat("objects left:", ls(), "\n")
The names are printed in alphabetical order, not in creation order.
rm(draft) removes only that binding; exists then
returns FALSE for the name. After the final removal the vector of
names is empty, so cat prints the label and nothing else. In an
interactive session, q() asks whether to save the workspace to
.RData, and the next session can restore it; a batch script
should not depend on that file.
$ Rscript workspace.R objects: count draft final after rm: count final draft exists: FALSE objects left:
A complete example: a project report
The final example puts the pieces together. The project has the
.Rprofile from the previous section, a file that defines the data,
and a report script that takes a title from the command line. The data file
contains no printing, so it can be sourced by any script that needs the same
numbers.
sales <- c(120, 90, 150, 80) region <- "north"
The report script reads the title, sources the data file, and prints a short
summary. It relies on project_name from the profile and on the
digits option, so the same script would print different precision in a
directory without that profile.
args <- commandArgs(trailingOnly = TRUE)
source("sales.R")
cat("report:", args[1], "\n")
cat("project:", project_name, "\n")
cat("region:", region, "\n")
cat("sales:", sales, "\n")
cat("total:", sum(sales), "\n")
cat("average:", mean(sales), "\n")
The profile line comes first, then the title and the project fields. The four sales values print on one line, and the total and the mean are computed with the functions introduced earlier. The mean is exactly 110 here, so the digits option does not change its appearance; a fractional mean would print four significant digits instead of seven.
$ Rscript report.R "autumn quarter" project profile loaded report: autumn quarter project: sales region: north sales: 120 90 150 80 total: 440 average: 110
Next steps
The help and documentation article shows how to look
up the functions used in these scripts, including commandArgs and
source. The variables article
covers assignment in more detail, and the
reading and writing data article replaces the
vectors defined in code with files read from disk.
Source
In this article we looked at the environment around an R script. The console
showed the primary and continuation prompts, and Rscript ran files
that read their command-line arguments. getwd,
setwd, list.files, and source moved
between directories and reused another file, while a project
.Rprofile set an option and a name before the first command.
ls and rm managed the workspace, and the final
report combined a profile, a sourced data file, and a title from the command
line. The help article takes the next step and shows
how to read the documentation for these functions.
Author
List all R Language tutorials.