Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in contrib.url(repos, "source") in R trying to use CRAN without setting a mirror Calls: install.packages -> contrib.url Execution halted

Tags:

package

r

I have some R code that executes perfectly in RStudio, but when I run it in cmd I get the below error .

Installing package into 'C:/Users/Anish/Documents/R/win-library/3.5'
(as 'lib' is unspecified)
Error in contrib.url(repos, "source") :
  trying to use CRAN without setting a mirror
Calls: install.packages -> contrib.url
Execution halted

I am including my package in my code as install.packages("plyr"). I have also tried using repos and source inside install.package function.

like image 827
anish shah Avatar asked Mar 19 '19 15:03

anish shah


People also ask

What is error in contrib url in R?

} The cause of the “error in contrib. url repos source r markdown” message is more than a simple matter of the R app software not being able to find the installed package. This code attempts the installation of a non-existing package.

What does error in contrib url mean?

The error means that your R session is attempting a package installation using the contrib. url package but the R-language options do not tell it where to get packages from.

What CRAN mirror should I use for R?

If you are downloading R from CRAN, the following CRAN mirrors support HTTPS and we recommend using one of them: CRAN master (Austria): https://cran.r-project.org/ RStudio (USA): https://cran.rstudio.com/ Revolution Analytics (USA): https://cran.revolutionanalytics.com/

What is a CRAN mirror?

CRAN is a network of ftp and web servers around the world that store identical, up-to-date, versions of code and documentation for R. Please use the CRAN mirror nearest to you to minimize network load.


2 Answers

Use

install.packages('plyr', repos = "http://cran.us.r-project.org")
like image 196
Anjani Avatar answered Oct 09 '22 16:10

Anjani


The reason is most likely because RStudio configures R so it knows how to to check for system packages and where to download them if not locally available, whereas from the command line your R is likely missing that configuration. Check with R.home(component = "home") from the command line. In the returned folder look for a file like Rprofile. In my system the line was commented (sight):

$ grep -i "options(repos" /usr/lib64/R/library/base/R/Rprofile
# options(repos = c(CRAN="@CRAN@"))

I experienced the same error re-running an R markdown document on a computer different from the one I had originally written it. To fix it I explicitly set the repos option in the first R chuck of the document and then knitr started to work instead of getting stuck in this error. The error means that your R session is attempting a package installation using the contrib.url package but the R-language options do not tell it where to get packages from.

Here is the line I introduced in the first R chunk of the Rmd document. I got the idea from https://github.com/eddelbuettel/littler/issues/23.

options(repos = list(CRAN="http://cran.rstudio.com/"))

This would give you a closer behaviour from the command line as from the RStudio environment in terms of package downloads anyway.

I suggest setting the option at the top of the script you are running. Of course, the best practice is to configure the R language to your expectations and share that configuration with your audience-users via the R.profile file, read how to customize R.

like image 28
Pablo Adames Avatar answered Oct 09 '22 15:10

Pablo Adames