簡體   English   中英

從命令行運行R命令

[英]Run R command from command line

有沒有辦法從命令行運行R命令? 就像是

$ R --run '1+1'
2

甚至喜歡

$ Rscript < '1+1'
2

命令行選項-e這樣做的。

Rscript.exe -e "1+1"

[1] 2

如果你只運行沒有參數的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

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

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

來自man R

   --slave
          Make R run as quietly as possible

   -e EXPR
          Execute 'EXPR' and exit

隨着littler安裝的路徑r配置正確,你可以這樣做:

echo 'print(1+1)' | r  
# [1] 2

注意, r不是拼寫錯誤。 這是來自小littler的命令。 它還支持像RRscript這樣的-e選項:

r -e 'print(1+1)'     
# [1] 2

它似乎只在您在表達式中明確調用print/cat/...print/cat/...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM