我試圖爲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版本。
任何建議,將不勝感激。
謝謝@Spacedman。從.libPaths()我的個人Rlibrary從Rscript路徑中丟失。添加到該位置的路徑與刪除--vanilla標誌一樣工作。 – haffamoto