我正在尝试使用shell命令在R中安装一些软件包。
我写了一个R文件“installDependencies.R
”来安装包。该文件如下:
packages <- c("xts","stringr","log4r")
# Function to check whether package is installed
is.installed <- function(mypkg){
is.element(mypkg, installed.packages()[,1])
}
for(package in packages){
# check if package is installed
if (!is.installed(package)){
install.packages(package)
}
}
现在我正在尝试使用终端运行此文件。我创建了一个shell脚本文件inst.sh
,如下所示:
#!/bin/bash
Rscript installDependencies.R
每当我使用./inst.sh
命令运行文件时,都会生成以下错误:
algotree@algotree-900X3C-900X4C-900X4D:~$ ./inst.sh
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Error in contrib.url(repos, type) :
trying to use CRAN without setting a mirror
Calls: install.packages -> grep -> contrib.url
Execution halted
algotree@algotree-900X3C-900X4C-900X4D:~$
答案 0 :(得分:3)
在设置CRAN镜像之前,我遇到了类似的问题
for(x in pkgs){
if(!is.element(x, installed.packages()[,1]))
{install.packages(x, repos="http://cran.fhcrc.org")
} else {print(paste(x, " library already installed"))}
}