问题描述
使用以下代码,我获得了所需的数据,但是由于某些原因,我无法弄清knitr
并不允许我编译PDF文档,如下所示:
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:
我的代码:
install.packages("weatherData")
library(weatherData)
istanbul <- getWeatherForDate("Istanbul",
start_date = Sys.Date() - 41,
end_date = Sys.Date())
正常工作,但尝试编译PDF时收到以下消息:
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
推荐答案
除非您特别要求,否则Knitr会产生一个R会话,而没有默认的cran镜像.我们往往会忘记使用Rstudio时需要为每个R会话设置CRAN,因为它会处理它,但仅用于交互用途,而不用于knitr.
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.
您可以尝试将镜像指定为install.packages
参数:
You could try specifying a mirror as a install.packages
argument:
install.packages("weatherData",repos = "http://cran.us.r-project.org")
或者,您可以在.Rprofile
中设置默认的CRAN镜像.参见此答案.
Alternatively, you could set up your default CRAN mirror in your .Rprofile
. See this answer.
也就是说,通过可能会多次编译的knitr文档安装软件包并不是一个好主意.您应该假设人们知道如何在需要时安装丢失的软件包,或者至少在再次安装之前测试该软件包是否已安装
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")
这篇关于Knitr文档中的install.packages失败:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!