简体   繁体   English

"install.packages 在 knitr 文档中失败:“尝试使用 CRAN 而不设置镜像”"

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

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:使用下面的代码我得到了我想要的数据,但由于某种原因我无法弄清楚knitr不允许我编译 PDF 文档,如下所示:

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:没有问题,但我在尝试编译 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 produces a R session, without a default cran mirror unless you specifically asked for one. Knitr 生成一个 R 会话,没有默认的 cran 镜像,除非你特别要求。 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.当我们使用 Rstudio 时,我们往往会忘记我们需要为每个 R 会话设置 CRAN,因为它会处理它,但仅用于交互式使用,而不用于 knitr。

You could try specifying a mirror as a install.packages argument:您可以尝试将镜像指定为install.packages参数:

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

Alternatively, you could set up your default CRAN mirror in your .Rprofile .或者,您可以在.Rprofile设置默认的 CRAN 镜像。 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 .也就是说,通过您可能会多次编译的 knitr 文档安装软件包并不是一个好主意 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")

You must set the CRAN repository in your R. To do so, launch R or RStudio.您必须在 R 中设置 CRAN 存储库。为此,请启动 R 或 RStudio。 in the R terminal run following codes.在 R 终端中运行以下代码。

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.上面的代码在 R 中定义了 CRAN 存储库,在接下来的包安装中无需再次定义。

Alternative way is to simply run install.packages("weatherData", repos="http://cran.us.r-project.org") .另一种方法是简单地运行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.但是,在第二种解决方案中,未设置存储库,您必须在每个软件包安装中将其作为参数传递。

Honestly,诚实地,

It would not work for me because I installed packages, and these lines of code were interrupting the knit function.它对我不起作用,因为我安装了软件包,而这些代码行中断了 knit 功能。 When I removed all lines containing installing packages (and used the most updated R and R Markdown available) this error went away.当我删除所有包含安装包的行(并使用最新的 R 和 R Markdown 可用)时,此错误消失了。

Ramin Ar's solution worked for me. Ramin Ar 的解决方案对我有用。

Basically, install your packages in the R console, not the R markdown.基本上,将包安装在 R 控制台中,而不是 R 降价。

"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R 中的 contrib.url(repos, "source") 中的错误尝试在不设置镜像的情况下使用 CRAN 调用:install.packages -&gt; contrib.url 执行停止 - Error in contrib.url(repos, "source") in R trying to use CRAN without setting a mirror Calls: install.packages -> contrib.url Execution halted install.packages()强制没有CRAN镜像选择? - install.packages() enforce no CRAN mirror selection? 在R中的install.packages()时选择CRAN镜像的目的? - Purpose of selecting a CRAN mirror when install.packages() in R? R - knitr - ShareLatex - install.packages(xyz) - R - knitr - ShareLatex - install.packages(xyz) install.packages("devtools") 失败 - install.packages("devtools") fails 尝试发布 R 笔记本并不断收到相同的错误(错误在 contrib.url(repos, “source”) 尝试使用 CRAN 而不设置镜像 - Trying to publish an R notebook and keep getting the same error (Error in contrib.url(repos, “source”) trying to use CRAN without setting a mirror 错误:“没有调用 package ...”并尝试使用 install.packages 来解决它 - Error: "there is no package called ..." and trying to use install.packages to solve it R:apt-get install r-cran-foo与install.packages(“foo”) - R: apt-get install r-cran-foo vs. install.packages(“foo”) 有没有办法在不运行 install.packages() 的情况下“安装”R 包? - Is there a way to 'install' R packages without running install.packages()? 强制install.packages() - Force install.packages()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM