2016-02-23 52 views
2

我試圖爲R-bloggers獲得的optparse包運行一些演示R代碼。我使用Ubuntu 14.04庫中的錯誤(optparse):沒有名爲「optparse」的包

的代碼是:

#!/usr/bin/env Rscript 
library(optparse) 

option_list = list(make_option(c("-f", "--file"), 
           type="character", default=NULL, 
           help="dataset file name",   
           metavar="character"), 
        make_option(c("-o", "--out"), 
           type="character", default="out.txt", 
           help="output file name [default=  
             %default]", metavar="character") 
       ); 

opt_parser = OptionParser(option_list=option_list); 
opt = parse_args(opt_parser); 

if (is.null(opt$file)){ 
    print_help(opt_parser) 
    stop("At least one argument must be supplied (input file).n",  
    call.=FALSE) 
} 

## program... 
df = read.table(opt$file, header=TRUE) 
num_vars = which(sapply(df, class)=="numeric") 
df_out = df[ ,num_vars] 
write.table(df_out, file=opt$out, row.names=FALSE) 

如果整個腳本保存在一個使用電話叫yasrs.R文件:

Rscript --vanilla yasrs.R 

應該返回的幫助信息。

我得到一個錯誤:

庫(optparse)RSCRIPT --vanilla yasrs.R錯誤:沒有包稱爲 'optparse'

我已經通過RStudio時安裝的程序包(optparse)編寫代碼並確保在從終端進行呼叫時安裝該代碼。終端和RStudio都運行相同的R版本。

任何建議,將不勝感激。

+0

謝謝@Spacedman。從.libPaths()我的個人Rlibrary從Rscript路徑中丟失。添加到該位置的路徑與刪除--vanilla標誌一樣工作。 – haffamoto

回答

2

RStudio在哪裏安裝optparse?從packageDescription("optparse")獲取。

然後在您的Rscript環境和RStudio環境中檢查.libPaths()的輸出。也許RStudio卡住了RScript看不到的地方。

然後檢查即使它們可能是R的相同版本,它們可能是兩個不同的安裝。 R.home()每個說什麼?

一個或多個這些東西會告訴你爲什麼它沒有找到它。解決方案可能是編寫並運行一個安裝它的RScript,然後你應該確信它將進入RScript將來可以找到的位置。