R for modellers - Vignette 24

R from the command line

Julien Arino

Department of Mathematics

University of Manitoba*




* The University of Manitoba campuses are located on original lands of Anishinaabeg, Cree, Oji-Cree, Dakota and Dene peoples, and on the homeland of the Métis Nation.

Running an R script from the command line

Why run from the command line?

  • R is a scripted language and when run from an R console, it is interactive

  • \(\implies\) what you did before is in memory of the session you are working in

  • This is frequent cause for errors when sharing your code with others: some things are defined in your script, others you did in the session but did not copy to the script

Different program options

When you install R, you install several programs

  • The executable file, which launches the interactive session
    • In Windows, you get a shortcut to a console version
  • A program called Rscript that allows you to run an .. R script from your OS’ command line

Run a script from the command line

Rscript script/path/script-file-name.R

or, from within the directory

Rscript script-file-name.R

No output by default

  • Contrary to the interactive version, if you want to see an output when running from the command line, you have to force it using commands like print or writeLines

  • Figures will also not be displayed, so you may want to save them