Explore Courses Blog Tutorials Interview Questions
+2 votes
in Data Science by (50.2k points)

I am using shell commands to install some packages in R.  I have an R file "installDependencies.R" for installing packages, this is the content of the file:

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)){




Next, I'm running this on the terminal. This is the shell script I created:


Rscript installDependencies.R

I get the following error while running the file:

[email protected]:~$ ./

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

[email protected]:~$ 

1 Answer

+2 votes
by (108k points)

I faced the same problem, you must set a CRAN mirror like so:

for(x in pkgs){

  if(!is.element(x, installed.packages()[,1]))

    {install.packages(x, repos="")

  } else {print(paste(x, " library already installed"))}


Browse Categories