我正在试着为我从R-bloggers那里得到的optparse包运行一些演示R代码。我使用的是ubuntu 14.04
代码是:
#!/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应返回帮助消息。
我得到一个错误:
Rscript --库中的vanilla yasrs.R错误(Optparse):没有名为‘optparse’的包
我在写代码时通过RStudio安装了这个包(optparse),也确保了在终端调用时安装了它。终端和RStudio都运行相同的R版本。
如有任何建议,我们将不胜感激。
发布于 2016-02-23 18:37:03
RStudio在哪里安装了optparse?从packageDescription("optparse")那里得到这个。
然后在Rscript环境和RStudio环境中检查.libPaths()的输出。也许RScript看不到RStudio把它贴在了什么地方。
然后检查,即使它们可能是相同版本的R,它们也可能是两个不同的安装。R.home()在每一项中都说了什么?
一个或多个这样的东西会告诉你为什么它找不到它。解决方案可能是编写并运行一个安装它的小RScript,然后你应该相当确定它将被放在RScript将来能找到它的位置。
https://stackoverflow.com/questions/35574783
复制相似问题