R
我经常在 bash 模式下运行脚本。我的脚本名为981_conduct_regression.R
.在此脚本中,我使用以下命令调用所需的包
if(!require(<package>)){
install.packages("<package>")
library(<package>)
}
现在,当我从 bash 模式(在 Ubuntu 14.04 上)调用脚本时,脚本(如下所示)无法安装软件包:
Loading required package: gridExtra
Installing package into ‘/home/michael/R/x86_64-pc-linux-gnu-library/3.1’
(as ‘lib’ is unspecified)
Error in contrib.url(repos, type) :
trying to use CRAN without setting a mirror
Calls: source ... eval -> eval -> install.packages -> grep -> contrib.url
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘gridExtra’
Execution halted
我必须改变什么才能让我的想法发挥作用?
编辑:这是.sh
文件:
#!/bin/bash
Rscript Code/981_conduct_regression.R
答案1
您需要指定您的CRAN镜像;在 R 中交互式运行
chooseCRANmirror()
选择合适的镜子,然后
options("repos")
查看生成的 URL。您可以将其永久添加到您的配置中~/.Rprofile
:
local({r <- getOption("repos")
r["CRAN"] <- "<URL from above goes here>"
options(repos=r)
})
答案2
根据 Stephen Kitt 的建议,这解决了问题:
~ $ cat .Rprofile
local({r <- getOption("repos")
r["CRAN"] <- "https://mirror.las.iastate.edu/CRAN/"
options(repos=r)
})