Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run R command from command line

Tags:

command-line

r

Is there a way to run an R command from the command line? Something like

$ R --run '1+1'
2

or even like

$ Rscript < '1+1'
2
like image 513
Hatshepsut Avatar asked Aug 22 '17 04:08

Hatshepsut


People also ask

How do I run an R program from the command line?

Starting R If R has been installed properly, simply entering R on the command line of a terminal should start the program. In Windows, the program is typically specified as the action performed when clicking on an icon. You can also use this method on a *NIX system that has a window manager such as KDE.

How do I run an R file in Windows command line?

To open up the command prompt, just press the windows key and search for cmd. When R is installed, it comes with a utility called Rscript. This allows you to run R commands from the command line.

How do I run an R command in bash?

Use Rscript to run R from bash You can run it using Rscript test. r . And even better, if you add an initial shebang line #!/usr/bin/env Rscript in the script above and make it executable with chmod +x test. r , you can directly launch your R script with ./test.

How do I get R in cmd?

Go to the command prompt [you can simply press the Down Arrow on your keyboard and your cursor will jump the command prompt]. Paste the code at the command prompt [use CNTL+v in windows or ⌘+v in Mac], then press ENTER. You can now interact with R using the command line interface.


2 Answers

The command line option -e does exactly that.

Rscript.exe -e "1+1"

[1] 2

It is clearly explained in the help which you get if you just run RScript without parameters:

Usage: /path/to/Rscript [--options] [-e expr [-e expr2 ...] | file] [args]

--options accepted are
  --help              Print usage and exit
  --version           Print version and exit
  --verbose           Print information on progress
  --default-packages=list
                      Where 'list' is a comma-separated set
                        of package names, or 'NULL'
or options to R, in addition to --slave --no-restore, such as
  --save              Do save workspace at the end of the session
  --no-environ        Don't read the site and user environment files
  --no-site-file      Don't read the site-wide Rprofile
  --no-init-file      Don't read the user R profile
  --restore           Do restore previously saved objects at startup
  --vanilla           Combine --no-save, --no-restore, --no-site-file
                        --no-init-file and --no-environ

'file' may contain spaces but not shell metacharacters
Expressions (one or more '-e <expr>') may be used *instead* of 'file'
See also  ?Rscript  from within R
like image 103
Andrey Shabalin Avatar answered Oct 27 '22 02:10

Andrey Shabalin


You can do it with the R command:

$ R --slave -e '1+1'
[1] 2

From man R:

   --slave
          Make R run as quietly as possible

   -e EXPR
          Execute 'EXPR' and exit
like image 42
Hatshepsut Avatar answered Oct 27 '22 02:10

Hatshepsut