77

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
CL.
  • 14,577
  • 5
  • 46
  • 73
Locksmith
  • 875
  • 1
  • 6
  • 7

3 Answers3

128

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")
scoa
  • 19,359
  • 5
  • 65
  • 80
  • 3
    Interesting - can you explain *why* there is no default CRAN mirror in the new environment? – CL. Nov 28 '15 at 10:55
  • 5
    I actually meant R session, not environment. Sorry for that. This is just a default R behavior we tend to forget when we use Rstudio, because it takes care of it. – scoa Nov 28 '15 at 11:04
  • 2
    No worrys, I was not confused about "environment" vs. "session" (this was clear from the context). However, I didn't know that RStudio is involved in setting such options. Thank you for the clarification. – CL. Nov 28 '15 at 11:23
  • 1
    You can convert the package install code into a comment. This eliminates the error and also gives the user a hint about the package installation. `#install.packages (" weatherData ", repos =" http://cran.us.r-project.org ")` – NCC1701 Jan 04 '21 at 13:52
  • 2
    I just started using R for the first time 5 minutes ago and the first thing I have to do is *Google an obscure error message so I can configure the package download mirror??* Wtf are they smoking? – Timmmm Mar 16 '21 at 16:25
28

You must set the CRAN repository in your R. To do so, launch R or RStudio. in the R terminal run following codes.

r = getOption("repos")
r["CRAN"] = "http://cran.us.r-project.org"
options(repos = r)
install.packages("weatherData")

Above code defines CRAN repository in the R and in next package installation no need to define again.

Alternative way is to simply run install.packages("weatherData", repos="http://cran.us.r-project.org"). However, with the second solution the repository not set and you must pass it as a parameter in every package installation.

Ramin Ar
  • 1,213
  • 13
  • 10
1

Honestly,

It would not work for me because I installed packages, and these lines of code were interrupting the knit function. When I removed all lines containing installing packages (and used the most updated R and R Markdown available) this error went away.

Dharman
  • 30,962
  • 25
  • 85
  • 135
John James
  • 13
  • 4