Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

install.packages fails in knitr document: "trying to use CRAN without setting a mirror"

Tags:

r

knitr

Using the following code I got the data I wanted, but for some reason I can't figure out knitr doesn't let me compile a PDF document, as shown further below:

My code:

install.packages("weatherData") library(weatherData) istanbul <- getWeatherForDate("Istanbul",                               start_date = Sys.Date() - 41,                                end_date = Sys.Date()) 

Works out with no problem but I get the following message trying compile the PDF:

Quitting from lines 3-31 (ist_weather.spin.Rmd)  Error in contrib.url(repos, type) :    trying to use CRAN without setting a mirror Calls: <Anonymous> ... eval -> eval -> install.packages -> grep -> contrib.url Execution halted 
like image 500
Locksmith Avatar asked Nov 28 '15 07:11

Locksmith


People also ask

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 are CRAN mirrors?

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.

What is knitr package?

The R package knitr is a general-purpose literate programming engine, with lightweight API's designed to give users full control of the output without heavy coding work. It combines many features into one package with slight tweaks motivated from my everyday use of Sweave.


1 Answers

Knitr produces a R session, without a default cran mirror unless you specifically asked for one. We tend to forget we need to set up CRAN for every R session when we use Rstudio because it takes care of it, but only for interactive use, not for knitr.

You could try specifying a mirror as a install.packages argument:

install.packages("weatherData",repos = "http://cran.us.r-project.org") 

Alternatively, you could set up your default CRAN mirror in your .Rprofile. See this answer.

That said, it is not a good idea to install packages through a knitr document that you will probably compile several times. You should assume people know how to install a missing package if needed, or at least test whether the package is installed before installing it again

if(!require(weatherData)) install.packages("weatherData",repos = "http://cran.us.r-project.org") 
like image 181
scoa Avatar answered Sep 24 '22 16:09

scoa