从命令行运行R命令

Hat*_*sut 7 command-line r

有没有办法从命令行运行R命令?就像是

$ R --run '1+1'
2
Run Code Online (Sandbox Code Playgroud)

甚至喜欢

$ Rscript < '1+1'
2
Run Code Online (Sandbox Code Playgroud)

And*_*lin 8

命令行选项就是-e这样做的.

Rscript.exe -e "1+1"

[1] 2
Run Code Online (Sandbox Code Playgroud)

如果你只是在RScript没有参数的情况下运行,你会得到清楚的解释:

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
Run Code Online (Sandbox Code Playgroud)


Hat*_*sut 6

您可以使用以下R命令执行此操作:

$ R --slave -e '1+1'
[1] 2
Run Code Online (Sandbox Code Playgroud)

来自man R

   --slave
          Make R run as quietly as possible

   -e EXPR
          Execute 'EXPR' and exit
Run Code Online (Sandbox Code Playgroud)