Question

I am trying to install some packages in R using shell commands. I wrote a R file "installDependencies.R" for installing packages. The file is as follows:

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

Now I am trying to run this file using terminal. I created a shell script file inst.sh and it is as follows:

#!/bin/bash

Rscript installDependencies.R

Whenever i run the file using ./inst.sh command, the following error generated :

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:~$ 
Was it helpful?

Solution

I had a similar issue before I set a CRAN mirror

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"))}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top