2012-05-15 28 views
13

我是外行人,希望unix和sofar我在Windows中使用R。例如,我在R會話中輸入以下內容(在R gui中)。在unix中爲unix-layman運行r腳本或命令與unix中的解釋器

# this is a my funny example script 
X <- 1:10 
Y <- 21:30 
plot(X, Y) 
myfun <- function (x){ 
       x1 <- x^0.2 
       return (x1) 
      } 
myfun(X) 

我怎樣才能在UNIX外殼實現這一點,在兩種情況下 -

(1)直接在經由interpeter 命令行(2)創建腳本和運行腳本。

請提供一步考慮我是外行unix。

+1

也許你應該用R進行Linux呢? –

+0

對不起,簡單的問題,linux和unix R有什麼區別?我相信我們可以在UNIX上運行R – SHRram

+1

你有什麼嘗試? R應該很好地安裝在unix或linux上,你可以通過命令行用'R'來訪問它。你也可以看看一些出色的guis(我會建議[RStudio](http://www.rstudio.org)作爲一個很好的起點)。最後,運行腳本可以輕鬆完成。通常使用'R CMD BATCH script.R',但有很多備選方案和選項都有詳細記錄。 – Justin

回答

25

假設您將腳本保存在名稱爲so.R的簡單文本文件中,可以在Linux/Unix下通過在提示符處輸入R來運行它。一旦R中輸入

source('so.R') 

執行腳本R環境裏面(這是假設so.R文件在同一目錄中,你是當發出這一命令)。

運行在Linux/Unix命令行使用以下命令腳本:

R CMD BATCH so.R 

注意,我得到的情節展現,當我跑的R裏面的腳本,但是從Linux命令行它不顯示。我懷疑它會很快顯示然後消失,所以會出現一個R命令,您必須查看它以使其在顯示圖表後暫停。

+4

您可能需要考慮實際腳本使用'Rscript'(附帶R)或舊版'r'(來自我們的軟件包小工具)。使用R CMD BATCH不贊成使用Rscript。如果你有它,r也很好。 –

1

我在猜測你的問題,你可能SSH到Linux機器?或者,例如,您在平常的筆記本電腦/ PC上安裝了Ubuntu。

假設是第二種情況:打開終端並鍵入sudo apt-get install r-base。然後鍵入R。然後鍵入

X <- 1:10 
Y <- 21:30 
plot(X, Y) 
myfun <- function (x){ 
       x1 <- x^0.2 
       return (x1) 
      } 
myfun(X) 

由於您的問題是關於unixlinux而非R,您也可以嘗試http://unix.stackexchange.com。關於linux和unix之間的區別有很多,但您可能需要知道的僅僅是:download Ubuntu,將其刻錄到光盤上,然後使用CD驅動器中的光盤重新啓動計算機。

希望這會有所幫助。

+0

它是_lowercase_ R:'sudo apt-get install r-base',因爲所有包名都是小寫。 –

+0

謝謝@DirkEddelbuettel。固定。 – isomorphismes

1

如果你的程序將在一個單一的數據集工作,那麼簡單-R可能是解決辦法:

http://code.google.com/p/simple-r/

它特別適用於簡單的統計分析,Linux命令行的部分設計。例如,如果想要繪製一些數據,'r -p data.txt'將完成這項工作;爲獲得相關係數:'r cor data.txt'就足夠了。

+0

幾年前,我們實際上已經佔用了/ usr/bin/r,因爲我們的littler項目比較通用。 –

1

以下示例顯示了在shell腳本中運行R代碼的兩種方法。如果通過source()函數將腳本 加載到交互式R會話中,則兩個 示例都將定義不執行它們的函數。

第一個示例允許您像給任何其他shell 腳本一樣提供參數,但不會將其他R選項傳遞給R(因爲Rscript將 「--args」指定爲R作爲參數之一) 。

第二個示例允許您提供其他R選項,但會生成 (無害)警告消息,除非將參數「--args」作爲腳本 之一。除非您有特殊要求,否則最好避免使用此版本。

原型Rscript.r

#!/usr/bin/env Rscript 
# Prototype R script for use at command line in Linux, Mac OS X, UNIX 

# References: 
# Manual "A Introduction to R", available via help.start() from the R Console 
# Appendix "B.1 Invoking R from the command line" in "A Inroduction to R", 

showArguments <- function(argv) { 
    print(argv) 
    0 
} 

if (! interactive()) { 
    # set some error return codes 
    SCRIPT_ERROR <- 10      # see documentation for quit() 
    SCRIPT_ARG_ERROR <- SCRIPT_ERROR + 1 

    # Define ARGV as script path concatenated to script arguments 
    ARGV <- commandArgs(FALSE)   # start with all the arguments given to R 
    scriptPath <- sub("^--file=", "", grep("^--file=", ARGV, value=TRUE)) [[1]] 
    ARGV <- c(scriptPath, commandArgs(TRUE)) 

    if (length(ARGV) < 2) { 
     cat(file=stderr(), sep="", 
      "Usage: ", ARGV[[1]], " [ options ] item ...\n", 
      "  Do something with item\n", 
      "  See script for details\n") 
     quit(save="no", status=SCRIPT_ARG_ERROR) 
    } 
    quit(save="no", status=showArguments(ARGV)) 
} 

原型shellscript.r

#!/usr/bin/env R --slave --vanilla --quiet -f 
# Prototype R script for use at command line in Linux, Mac OS X, UNIX 

# References: 
# Manual "A Introduction to R", available via help.start() from the R Console 
# Appendix "B.1 Invoking R from the command line" in "A Inroduction to R", 

showArguments <- function(argv) { 
    print(argv) 
    0 
} 

if (! interactive()) { 
    # set some error return codes 
    SCRIPT_ERROR <- 10      # see documentation for quit() 
    SCRIPT_ARG_ERROR <- SCRIPT_ERROR + 1 

    # Define ARGV as the arguments given to this script (after argument 「-f」) 
    ARGV <- commandArgs(FALSE)   # start with all the arguments given to R 
    ARGV <- ARGV[(grep("-f", ARGV) [[1]] + 1):length(ARGV)] 
    if (any(grepl("--args", ARGV))) { # remove arguments intended only for R 
     ARGV <- c(ARGV[[1]], commandArgs(TRUE)) 
    } 

    if (length(ARGV) < 2) { 
     cat(file=stderr(), sep="", 
      "Usage: ", ARGV[[1]], " [ R_options ] --args [ options ] item ...\n", 
      "  Do something with item\n", 
      "  See script for details\n") 
     quit(save="no", status=SCRIPT_ARG_ERROR) 
    } 
    quit(save="no", status=showArguments(ARGV)) 
}