R 與其他語言的連結

統計軟體 R

簡介

安裝

操作方式

變數與運算

有序數列

向量

矩陣

多維陣列

複數

因子

串列

資料框

時間數列

流程控制

輸出入

呼叫

函數

2D 繪圖

3D 繪圖

互動介面

套件列表

其他語言呼叫

R 的應用

集合

邏輯推論

模糊邏輯

機率邏輯

檢定

搜尋

優化算法

線性代數

決策樹

人工智慧

分群分類

SVM 向量機

神經網路

遺傳演算法

資料採礦

訊號處理

影像處理

語音處理

自然語言

機器學習

機器人

生物統計

數位訊號處理

方程式求解

數值分析

微積分

微分方程

線性規劃

圖形理論

統計推論

字串處理

正規表示式

視窗程式

網頁程式

文件格式

貝氏網路

訊息

機率統計書

相關網站

參考文獻

最新修改

簡體版

English

從命令列呼叫 R 程式。

C:\Program Files\R\R-2.13.1\bin>R
> x=c(1,2,3)
> x
[1] 1 2 3
> q()
儲存工作空間圖案? [y/n/c]: n

C:\Program Files\R\R-2.13.1\bin>

如果要從命令列執行 R 程式,可以用下列方法。

檔案:d:\code\R\test.R

x=c(1,2,3)
x

執行命令:

C:\Program Files\R\R-2.13.1\bin>R --no-save < d:\code\R\test.R
> x=c(1,2,3)
> x
[1] 1 2 3
>

C:\Program Files\R\R-2.13.1\bin>
C:\Program Files\R\R-2.13.1\bin>R CMD BATCH --help
Usage: R CMD BATCH [options] infile [outfile]

Run R non-interactively with input from infile and place output (stdout
and stderr) to another file.  If not given, the name of the output file
is the one of the input file, with a possible '.R' extension stripped,
and '.Rout' appended.

Options:
  -h, --help            print short help message and exit
  -v, --version         print version info and exit
  --no-timing           do not report the timings
  --                    end processing of options

Further arguments starting with a '-' are considered as options as long
as '--' was not encountered, and are passed on to the R process, which
by default is started with '--restore --save'.

Report bugs to <r-bugs@r-project.org>.

C:\Program Files\R\R-2.13.1\bin>R CMD BATCH d:/code/R/test.R

結果可以在 D:\code\R 資料夾下看到一個 test.Rout 的檔案,內容如下:

> x=c(1,2,3)
> x
[1] 1 2 3
> 
> proc.time()
   user  system elapsed 
   0.40    0.06    0.43 
錯誤在gzfile(file, "wb") : 無法開啟連結
Calls: sys.save.image -> save.image -> save -> gzfile
此外: Warning message:
In gzfile(file, "wb") :
  cannot open compressed file '.RDataTmp', probable reason 'Permission denied'
停止執行
Warning message:
In file.remove(outfile) :
  cannot remove file '.RDataTmp', reason 'No such file or directory'

如果要用程式以命令列呼叫 R 程式,可以參考下列文章

I’m writing this post because I just spent a couple of hours banging my head against the wall, trying to figure out how to run an R script from the command line. It was working if I simply ran it at the command line. But when I try to run the same command from Java (I know, this sounds convoluted) using the following code, it was behaving strangely and wasn’t executing. (錯誤方法)

Runtime r = Runtime.getRuntime();
r.exec("R CMD BATCH RScriptFile.R");

So I finally found out that there is a utility in R that is designed to help you execute scripts at the command line more easily. It’s called RScript. Now I’m doing it the following way, and it’s working beautifully. (正確方法)

Runtime r = Runtime.getRuntime();
r.exec("Rscript RScriptFile.R");

Seems obvious, but it wasn’t to me, and it took awhile to find an answer.

呼叫系統函數 (R 與外部程式)

相反的,如果要從 R 呼叫系統程式,可以參考下列文章:

  1. https://sites.google.com/site/rprojectnotes/advanced_r/running_external_programs
  • Linux :

output <- system("ls", intern = TRUE)

  • MS. Windows :

shell("dir > c:/dir.txt")
shell.exec("c:/dir.txt")
shell.exec("http://sites.google.com/site/rprojectnotes/")

與 Python 連結

  1. 參看:统计学与R读书笔记第四版 — http://www.rproject.info/uploads/Site/R.pdf
    1. Chapter 6 在python中调用R(rpy2)

與 Perl 連結

  1. https://sites.google.com/site/rprojectnotes/advanced_r/r_and_perl

與 PHP 連結

  1. https://sites.google.com/site/rprojectnotes/advanced_r/r_and_php

與 C 連結

  1. https://sites.google.com/site/rprojectnotes/advanced_r/r_and_c

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License