How to use R from a shell (including the Windows command-line / / Unix terminal).
Under Unix-alikes, it is easy to invoke an R session from a shell by typing the name of the R executable you wish to run. On Windows, you should see that typing the name of the R executable you wish to run does not run that application, but instead signals an error. Instead, you will have to type the full path of the directory where your R executables are located (see section Where are my R executable files located?), followed by the name of the R executable you wish to run.
This is not very convenient to type everytime something needs to be run from
a shell, plus it has another issue of being computer dependent. The solution
is to add the path of the directory where your R executables are located to
the Path
environment variable. The Path
environment variable is a
list of directories where executable programs are located. When you type the
name of an executable program you wish to run, Windows looks for that program
through each directory in the Path
environment variable. When you add
the full path of the directory where your R executables are located to your
Path
environment variable, you should be able to run any of those
executable programs by their basenames (‘R’, ‘Rcmd’,
‘Rscript’, and ‘Rterm’) instead of their full paths.
To add a new path to your Path
environment variable:
Open the Windows search bar (press Windows key)
Type Edit environment variables for your account
Click the variable Path
Click the button Edit...
Click the button New
Type (or paste) the full path of the directory where your R executables are located and click the OK button
This will modify your environment variable Path
, not the systems. If
another user wishes to run R from a shell, they will have to add the
directory to their Path
environment variable as well.
If you wish to modify the system environment variable Path
(you will
need admin permissions):
Open the Windows search bar (press Windows key)
Type Edit the system environment variables
Click the button Environment Variables...
Click the variable Path
in section System variables
Click the button Edit...
Click the button New
Type (or paste) the full path of the directory where your R executables are located and click the OK button
To check that this worked correctly, open a shell and execute the following commands:
R --help
R --version
You should see that the first prints the usage message for the R executable while the second prints information about the version of R currently being run. If you have multiple versions of R installed, make sure this is the version of R you wish to run.
In an R session, you can find the location of your R executable files with the following command:
R.home("bin")
For me, this is:
/usr/lib/R/bin
C:/PROGRA~1/R/R-
getRversion()/bin/x64
For the purpose of running R scripts, there are four ways to do it. Suppose our R script has filename script1.R, we could write any of:
R -f script1.R
R --file=script1.R
R CMD BATCH script1.R
Rscript script1.R
The first two are different ways of writing equivalent statements. The third
statement is the first statement plus options --restore
--save (plus option --no-readline under Unix-alikes), and
it also saves the stdout
and
stderr
in a file of your choosing. The
fourth statement is the second statement plus options --no-echo
--no-restore. You can try:
R --help
R CMD BATCH --help
Rscript --help
for a help message that describes what these options mean. In general,
Rscript
is the one you want to use. It should be noted that
Rscript
has some exclusive
environment variables
(not used by the other
executables) that will make its behaviour different from R
.
For the purpose of making packages, R CMD
is what you will need.
Most commonly, you will use:
R CMD build
R CMD INSTALL
R CMD check
R CMD build
will turn an R package (specified by a directory) into
tarball. This allows for easy sharing of R packages with other people,
including
submitting a package to CRAN.
R CMD INSTALL
will install an R package (specified by a directory
or tarball), and is used by
install.packages()
.
R CMD check
will check an R package (specified by a tarball) for
possible errors in code, documentation, tests, and much more.
If, when you execute one of the previous commands, you see the following error message: “‘R’ is not recognized as an internal or external command, operable program or batch file.”, see section Ease of Use on Windows.