Bashモードで実行したときに欠落しているRパッケージをインストールする

Bashモードで実行したときに欠落しているRパッケージをインストールする

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

Rで対話的に実行されるCRANイメージを指定する必要があります。

chooseCRANmirror()

正しいミラーを選択して

options("repos")

生成されたURLを確認してください。これを設定に永久に追加できます~/.Rprofile

local({r <- getOption("repos")
   r["CRAN"] <- "<URL from above goes here>" 
   options(repos=r)
})

おすすめ記事