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

后端 未结 2 1954
有刺的猬
有刺的猬 2020-11-29 05:30

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:<

相关标签:
2条回答
  • 2020-11-29 06:09

    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")
    
    0 讨论(0)
  • 2020-11-29 06:12

    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.

    0 讨论(0)
提交回复
热议问题